Using coroutines for async work
The first thing that we have to do is identify the async/heavy work that we have done in our Restaurants application.
Without looking at the code, we know that our app retrieves a list of restaurants from the server. It does that by initiating a network request with Retrofit and then waits for a response. This action qualifies as an async job because we don't want to block the main (UI) thread while the app waits for the network response to arrive.
If we check out the RestaurantsViewModel
class, we can identify that the getRestaurants()
method is the one place in our application where heavy blocking work is happening:
private fun getRestaurants() { restaurantsCall = restInterface.getRestaurants() restaurantsCall.enqueue(object : Callback <List<Restaurant>> { override...