使用协程在 Room 的 ViewModel 中显示 LiveData
Displaying LiveData in ViewModel from Room with co-routines
这个问题是关于 Android 使用 MVVM、LiveData、Room(以及 RetroFit)和协程进行编程的最佳实践。最佳实践之一指出
Long running executions such as Network - or Database calls should be performed asynchronous from the UI thread.
当前的文档和博客解释了如何使用协程详细地做到这一点,一些例子很好地证明了这一点,例如Sunflower App。
我缺少的部分是当 ViewModel 被初始化并且它需要显示来自 database/repository/network 的内容时,如何使用协同例程完成加载。在 Sunflower App 存储库中 returning LiveData,但没有使用协程。
示例:
在PlantDao
中我们看到:
@Query("SELECT * FROM plants WHERE id = :plantId")
fun getPlant(plantId: String): LiveData<Plant>
没有 suspend
关键字,因此,这不是协同例程的一部分。
在plantRepository
中有:
fun getPlant(plantId: String) = plantDao.getPlant(plantId)
同样没有 suspend
关键字,所以没有协程。
在PlantDetailViewModel
中初始化显示给我们
val plant = plantRepository.getPlant(plantId)
所以没有作用域、作业或任何与协程相关的东西。
我的问题:
- 房间执行数据库查询是异步的吗?如果是,是否使用协程?
- 这是一个好习惯吗?因为 repo 只是 returning LiveData 并且只能用于 return LiveData
- 还有什么其他策略可以做到这一点?有什么例子吗?
- 对于网络请求,此策略会有所不同吗?
Is room performing the DB query asynchronous? And if so, is it using co-routines?
它正在执行异步,不,它不使用协程。 LiveData 是生命周期感知的,因此只有当它被恢复的 LifecycleOwner 观察到时才会被调用,比如 Fragment。
Is this a good practice? Because the repo is only returning LiveData and can only be used to return LiveData
有点。如果您观看 https://www.youtube.com/watch?v=zbYYoL7vo9Y and https://www.youtube.com/watch?v=B8ppnjGPAGE,您会发现他们正在避免在您的存储库或数据源中使用 LiveData,而是在这些层中使用协程。重要的是要了解您的调用属于哪个协程范围。 F.i.,用户看不到结果是不是就结束了?
What are other strategies to do this? Any examples? Would this strategy differ for network requests?
Android镇上的新热点是协程结合Flow使用。如果您使用 Retrofit 进行网络调用,它现在也支持协程。这是一个不错的代码实验室:https://codelabs.developers.google.com/codelabs/kotlin-coroutines/#0
这个问题是关于 Android 使用 MVVM、LiveData、Room(以及 RetroFit)和协程进行编程的最佳实践。最佳实践之一指出
Long running executions such as Network - or Database calls should be performed asynchronous from the UI thread.
当前的文档和博客解释了如何使用协程详细地做到这一点,一些例子很好地证明了这一点,例如Sunflower App。
我缺少的部分是当 ViewModel 被初始化并且它需要显示来自 database/repository/network 的内容时,如何使用协同例程完成加载。在 Sunflower App 存储库中 returning LiveData,但没有使用协程。
示例:
在PlantDao
中我们看到:
@Query("SELECT * FROM plants WHERE id = :plantId")
fun getPlant(plantId: String): LiveData<Plant>
没有 suspend
关键字,因此,这不是协同例程的一部分。
在plantRepository
中有:
fun getPlant(plantId: String) = plantDao.getPlant(plantId)
同样没有 suspend
关键字,所以没有协程。
在PlantDetailViewModel
中初始化显示给我们
val plant = plantRepository.getPlant(plantId)
所以没有作用域、作业或任何与协程相关的东西。
我的问题:
- 房间执行数据库查询是异步的吗?如果是,是否使用协程?
- 这是一个好习惯吗?因为 repo 只是 returning LiveData 并且只能用于 return LiveData
- 还有什么其他策略可以做到这一点?有什么例子吗?
- 对于网络请求,此策略会有所不同吗?
Is room performing the DB query asynchronous? And if so, is it using co-routines?
它正在执行异步,不,它不使用协程。 LiveData 是生命周期感知的,因此只有当它被恢复的 LifecycleOwner 观察到时才会被调用,比如 Fragment。
Is this a good practice? Because the repo is only returning LiveData and can only be used to return LiveData
有点。如果您观看 https://www.youtube.com/watch?v=zbYYoL7vo9Y and https://www.youtube.com/watch?v=B8ppnjGPAGE,您会发现他们正在避免在您的存储库或数据源中使用 LiveData,而是在这些层中使用协程。重要的是要了解您的调用属于哪个协程范围。 F.i.,用户看不到结果是不是就结束了?
What are other strategies to do this? Any examples? Would this strategy differ for network requests?
Android镇上的新热点是协程结合Flow使用。如果您使用 Retrofit 进行网络调用,它现在也支持协程。这是一个不错的代码实验室:https://codelabs.developers.google.com/codelabs/kotlin-coroutines/#0