android - 房间返回 LiveData 后,检查是否需要更新
问题描述
如标题所述,我有 Room Database 将 LiveData 返回到 Repository 的场景。Repository 对 ViewModel 做同样的事情:
suspend fun getData(key: Long): LiveData<CustomData> = withContext(Dispatchers.IO) {
return@withContext myDao.getDataByKey(key)
}
ViewModel 从 switchMap 获取数据:
val data = anotherValue.switchMap { key ->
liveData {
emitSource(repository.getData(key))
}
}
那行得通。但是现在我想,一旦返回数据,或者就在之前,检查该数据是否足够旧以检查新数据(改造)。
我知道如何从服务中获取它,但找不到如何正确启动该更新。如果我检查存储库中的数据,在将其发送到 ViewModel 之前,从 DAO 返回的值始终为空。而且从 ViewModel 中,无法弄清楚该怎么做。
另一种选择可能是从片段中进行,因为我已经在观察 LiveData。但是希望尽可能保持清洁,而且我认为这不是一个好习惯。
解决方案
推荐阅读
- javascript - 函数在 React/Redux 中返回 [object Object] 而不是 html
- powershell - SqlClient.ExecuteNonQuery 不返回行数
- android - Zygote The String#value field is not present on Android versions >= 6.0 Android error
- javascript - Javascript setTimeout 立即在 React Native 中运行
- java - 杰克逊无法反序列化带有连字符的字段
- sql - 如何编写单个 SQL 查询以挑出具有多个条件的子组
- ruby-on-rails - 带有多个文件的 Rails file_field_tag 向控制器提供随机字符串
- haskell - blaze-html,Haskell,正确的 toHtml 用法?
- mysql - t2.micro EC2 实例运行速度过慢一段时间后恢复正常
- overheating - 戴尔l502x过热多年