- mongodb - 在 MongoDB mapreduce 中,如何展平值对象?
- javascript - 对象传播与 Object.assign
- html - 输入类型 ="submit"Vs 按钮标签它们可以互换吗?
- sql - 使用 MongoDB 而不是 MS SQL Server 的优缺点
显然,Room 无法处理 MutableLiveData,我们必须坚持使用 LiveData,因为它返回以下错误:
error: Not sure how to convert a Cursor to this method's return type
我以这种方式在我的数据库助手中创建了一个“自定义”MutableLiveData:
class ProfileRepository @Inject internal constructor(private val profileDao: ProfileDao): ProfileRepo{
override fun insertProfile(profile: Profile){
profileDao.insertProfile(profile)
}
val mutableLiveData by lazy { MutableProfileLiveData() }
override fun loadMutableProfileLiveData(): MutableLiveData<Profile> = mutableLiveData
inner class MutableProfileLiveData: MutableLiveData<Profile>(){
override fun postValue(value: Profile?) {
value?.let { insertProfile(it) }
super.postValue(value)
}
override fun setValue(value: Profile?) {
value?.let { insertProfile(it) }
super.setValue(value)
}
override fun getValue(): Profile? {
return profileDao.loadProfileLiveData().getValue()
}
}
}
这样,我从数据库获取更新,可以保存 Profile
对象,但我不能修改属性。
例如:mutableLiveData.value = Profile()
会起作用。mutableLiveData.value.userName = "name"
会调用 getValue()
而不是 postValue()
并且不起作用。
有人找到解决方案了吗?
最佳答案
说我疯了,但 AFAIK 没有理由为您从 DAO 收到的对象使用 MutableLiveData。
这个想法是您可以通过 LiveData<List<T>>
公开对象
@Dao
public interface ProfileDao {
@Query("SELECT * FROM PROFILE")
LiveData<List<Profile>> getProfiles();
}
现在你可以观察它们了:
profilesLiveData.observe(this, (profiles) -> {
if(profiles == null) return;
// you now have access to profiles, can even save them to the side and stuff
this.profiles = profiles;
});
因此,如果您想让这些实时数据“发出新数据并对其进行修改”,则需要将配置文件插入数据库。写入将重新评估此查询,并在新的配置文件值写入 db 后发出。
dao.insert(profile); // this will make LiveData emit again
所以没有理由使用 getValue
/setValue
,只需写入您的数据库即可。
关于android - 将 LiveData 转换为 MutableLiveData,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50943919/
我如何转换 MutableLiveData至 MutableLiveData . val text = NonNullMutableLiveData("") 我的类 NonNullMutableLi
private val _users = MutableLiveData>() val users: LiveData> get() = _users fun getUsers() { vie
我在 ViewModel 中有一个函数“A”,它从 firebase 检索数据,并将该值分配给 MutableLiveData (所有这些都包含在 onSuccessListener 中)并返回。此函
我有带有编辑文本和 2 个按钮的简单 View ,我希望用户能够使用按钮将数量增加/减少 1,或者他可以在编辑文本小部件中手动写入数量,但这个值不能小于 0 并且大于10. 我正在使用带有 LiveD
我正在使用 MutableLiveData 开发一个 Activity ,以使我的 fragment 与从搜索检索到的结果保持同步,但在更新结果列表后不会触发观察者。 我在主要 Activity 上有
我对 Viewmodel 中的 MutableLiveData 有疑问。 MutableLiveData 的 setValue 功能是否触发观察?如果我们更改 MutableLiveData 的内容而
我在应用的架构组件中使用 LiveData 和 ViewModel。 我有一个分页的项目列表,当用户向下滚动时我加载更多。查询的结果存储在一个 MutableLiveData> 当我执行初始加载并将变
如何使用初始值初始化 MutableLiveData?我正在寻找类似的东西: val text = MutableLiveData("initial value") 最佳答案 MutableLiveD
我在尝试更新我的 MutableLiveData 时遇到问题。我正在从我的 ViewModel 调用登录函数来更新我的 UI。在我的 ViewModel 中,我调用了我的 API 服务器,但是当我调用
我在 MVVM 架构上使用多个 MutableLiveData。 在 ViewModel 上,我发布了对象,但 fragment 没有恢复。 当 fragment 恢复时,观察者得到 MutableL
class Foo : ViewModel() { val bars: MutableLiveData> = MutableLiveData() get() { if(f
如何将新项目添加到 MutableLiveData 列表?我想构建一个无限滚动的 Recyclerview。所以我有一个包含 10 个项目的列表,在单击“加载更多按钮”后,我想添加 10 个新项目。
我在我的应用程序中使用实时数据,并且我有一个从 ViewModel 扩展的 View 模型. 在我的 View 模型中,我有一个列表: var songs: MutableLiveData> = Mu
我有 MyViewModel类变量 MutableLiveData items哪些商店,列表Item类(class)。还有功能fetchData()通过 Retrofit 库和 checkStatus
我有两个非常相似的函数,我试图通过使用泛型来避免代码中的重复。这些函数都有一个 try catch block ,并使用两种不同类型的两个 MutableLiveData 通知其观察者: val no
我观察到 MutableLiveData 会触发观察者的 onChanged,即使向其 setValue 方法提供相同的对象实例也是如此。 //Fragment#onCreateView - scen
更新: 如果我移动到另一个 fragment 并返回到这个 fragment ,则 TextView 会更新...... 我无法通过使用 setValue() 或 postValue() 让 UI 中
我发现了新的 Android 架构组件,我想通过一个小型测试应用程序来测试 ViewModel/LiveData 。后者有两个 fragment (在 ViewPager 中),第一个创建/更新卡片列
我现在陷入困境,目前想知道为什么我的可变数组列表即使使用postvalue()更新也返回null。我尝试使用Toast显示它,并且显示[],我认为它为null。它之间没有空间,因此看起来像一个盒子。我
在 Kotlin 中,声明变量类型时,使用冒号。即使在声明 LiveData 时,也会使用冒号。那么为什么 等号 用于 MutableLiveData?我一直无法弄清楚这一点。几天前我花了大约 2 个
我是一名优秀的程序员,十分优秀!