This question is in regard to the best practices in Android programming using MVVM, LiveData, Room (and hence also RetroFit) and co-routines. One of the best practises states that
Long running executions such as Network - or Database calls should be performed asynchronous from the UI thread.
Current documentation and blogs explain how to do this in detail using co-routines and some examples are demonstrating this nicely, e.g. the Sunflower App.
The part I am missing is when a ViewModel is initialized and it needs to show content from the database/repository/network, how the loading using co-routines is done. In the Sunflower App the repository is returning LiveData, but there is no use of co-routines.
Example:
In PlantDao we see:
@Query("SELECT * FROM plants WHERE id = :plantId")
fun getPlant(plantId: String): LiveData<Plant>
There is no suspend keyword hence, this is not part of a co-routine.
In plantRepository there is:
fun getPlant(plantId: String) = plantDao.getPlant(plantId)
Again no suspend keyword so no co-routine.
In the PlantDetailViewModel the initialization shows us
val plant = plantRepository.getPlant(plantId)
So no scope, Job or any co-routine related stuff.
My questions:
- Is room performing the DB query asynchronous? And if so, is it using co-routines?
- Is this a good practice? Because the repo is only returning LiveData and can only be used to return LiveData
- What are other strategies to do this? Any examples?
- Would this strategy differ for network requests?