如何解决在查询更改之前,来自房间查询的Livedata不会检测到新添加的条目
因此,无论何时将应用程序安装在设备上,它都会运行一个工作程序来进行数据库的初始植入。这样做很好,但是播种完成后,应该检索值的livedata查询不会更新。只有在视图模型中更改查询后,它才能正确更改。我不知道为什么。任何建议将不胜感激!
片段
viewmodel.sortedItems.observe(viewLifecycleOwner){
..
// Refreshes recyclerview
}
Viewmodel
var searchQuery = MutableLiveData<String>("")
val filtered = Transformations.switchMap(searchQuery){repository.getItems(it)}
val sortedItems = MediatorLiveData<List<Restaurant>>()
init{
sortedItems.addSource(filtered){sortData(it)}
}
存储库
fun getItems(query: String): LiveData<List<Item>> = itemDao.retrieveAll(query)
ItemDao
@Query("SELECT * FROM Item WHERE title LIKE :query || '%'"
fun retrieveAll(query: String): LiveData<List<Item>>
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。