为什么 MediatorLiveData 没有观察到两个不同的来源?
Why MediatorLiveData does not observe two different sources?
我有两个房间查询。
@Query("Select * from accountGroup")
fun getAll(): LiveData<List<AccountGroupDto>>
@Query("Select * from accounts")
fun getAll(): LiveData<List<AccountDto>>
对于这 2 个结果列表,我想创建一个新的 ShareItem 列表
data class ShareItem
(val accountId: Int, val accountGroupId: Int, val isGroup: Boolean)
我想在我的 activity 中观察它的组合列表。
所以我有实现 MediatorLiveData 的存储库,它添加了两个源,我希望每个项目都进行转换(创建 ShareItem)并将其添加到 mutableLiveData 并将其提供给 viewModel。
override fun getShareItems(): LiveData<List<Int>> {
val shareItems = MutableLiveData<List<ShareItem>>()
val liveDataMerger = MediatorLiveData<List<Int>>()
liveDataMerger.addSource(accountGroupsDao.getAll()) { groups ->
// Transform each element in ShareItem ??
println("Group log")
}
liveDataMerger.addSource(accountsDao.getAll()) { accounts ->
// Transform each element in ShareItem ??
println("Account log")
}
return shareItems
}
但是我从未在控制台中看到 System.out。既然数据库显然充满了数据,我还缺少什么?
However i never see the System.out in the console. What am i missing
since the database clearly is full of data?
有Observer
吗? LiveData
只有在至少有一个 Observer
.
时才开始工作
问题已解决。我从未注册过 Livedata 的观察员。
我有两个房间查询。
@Query("Select * from accountGroup")
fun getAll(): LiveData<List<AccountGroupDto>>
@Query("Select * from accounts")
fun getAll(): LiveData<List<AccountDto>>
对于这 2 个结果列表,我想创建一个新的 ShareItem 列表
data class ShareItem
(val accountId: Int, val accountGroupId: Int, val isGroup: Boolean)
我想在我的 activity 中观察它的组合列表。 所以我有实现 MediatorLiveData 的存储库,它添加了两个源,我希望每个项目都进行转换(创建 ShareItem)并将其添加到 mutableLiveData 并将其提供给 viewModel。
override fun getShareItems(): LiveData<List<Int>> {
val shareItems = MutableLiveData<List<ShareItem>>()
val liveDataMerger = MediatorLiveData<List<Int>>()
liveDataMerger.addSource(accountGroupsDao.getAll()) { groups ->
// Transform each element in ShareItem ??
println("Group log")
}
liveDataMerger.addSource(accountsDao.getAll()) { accounts ->
// Transform each element in ShareItem ??
println("Account log")
}
return shareItems
}
但是我从未在控制台中看到 System.out。既然数据库显然充满了数据,我还缺少什么?
However i never see the System.out in the console. What am i missing since the database clearly is full of data?
有Observer
吗? LiveData
只有在至少有一个 Observer
.
问题已解决。我从未注册过 Livedata 的观察员。