[英]Lazy initialization with repository
我查看了Android Sunflower最佳实践应用程序。 我有点难以理解为什么PlantDetailsViewModel的初始化有效。 该类定义如下
class PlantDetailViewModel @AssistedInject constructor(
plantRepository: PlantRepository,
private val gardenPlantingRepository: GardenPlantingRepository,
@Assisted private val plantId: String
) : ViewModel() {
val isPlanted = gardenPlantingRepository.isPlanted(plantId)
val plant = plantRepository.getPlant(plantId)
....
@AssistedInject.Factory
interface AssistedFactory {
fun create(plantId: String): PlantDetailViewModel
}
companion object {
fun provideFactory(
assistedFactory: AssistedFactory,
plantId: String
): ViewModelProvider.Factory = object : ViewModelProvider.Factory {
@Suppress("UNCHECKED_CAST")
override fun <T : ViewModel?> create(modelClass: Class<T>): T {
return assistedFactory.create(plantId) as T
}
}
}
}
在这里, gardenPlantingRepository
和plantRepository
可以访问Room Database 。 我想知道这是否有效,因为通常无法访问主线程上的房间数据库。 Viemodel 用于PlantDetailFragment并进行延迟初始化。
@Inject
lateinit var plantDetailViewModelFactory: PlantDetailViewModel.AssistedFactory
private val plantDetailViewModel: PlantDetailViewModel by viewModels {
PlantDetailViewModel.provideFactory(
plantDetailViewModelFactory,
args.plantId
)
}
如果我尝试一些完全相同的东西,我总是会遇到无法访问主线程上的数据库的问题。 所以我试图用coroutine
和Dispatchers.IO
在init
函数中初始化我的变量,但问题是当我访问我的视图模型的成员变量时,它们没有被初始化。 那么向日葵应用程序的行为如何重现
Repository 使用返回 LiveData 的 Dao 方法
Room 持久性库支持可观察查询,它返回 LiveData 对象。 可观察查询是作为数据库访问对象 (DAO) 的一部分编写的。
当数据库更新时,Room 会生成所有必要的代码来更新 LiveData 对象。 生成的代码在需要时在后台线程上异步运行查询。 此模式可用于使 UI 中显示的数据与存储在数据库中的数据保持同步。 https://developer.android.com/topic/libraries/architecture/livedata#use_livedata_with_room
如果你想使用协程,那么你应该在你的ViewModel
创建和公开LiveData
class ViewModel(
val repository: Repository
) : ViewModel() {
private val _liveData = MutableLiveData<SomeType>()
val liveData: LiveData<SomeType> get() = _liveData
init {
viewModelScope.launch(Dispatchers.IO) {
_liveData.postValue(repository.getSomeData())
}
}
}
然后你应该在你的活动/片段中观察这个 liveData
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.