[英]How does exposed LiveData dispatch changes when postValue is called on MutableLiveData variable?
In ViewModel:在视图模型中:
private var _someData : MutableLiveData<Boolean> = MutableLiveData()
var someData: LiveData<Boolean> = _someData
public fun someMethod(){
_someData.postValue(true)
}
In Fragment:在片段中:
viewModel.someData.observe(this, Observer {
//change posted on MutableLiveData but this LiveData received the changes.
})
Since someData
and _someData
are 2 different variables, how the onChanged()
method of someData
is invoked even if value is posted on _someData
and changes are observed for exposed LiveData
?由于
someData
和_someData
是 2 个不同的变量,如何调用someData
的onChanged()
方法,即使值发布在_someData
上并且观察到暴露的LiveData
的变化?
Both variables reference the same object.两个变量引用同一个对象。 The only difference is that you are exposing a
LiveData
so clients can't modify its value.唯一的区别是您公开了一个
LiveData
,因此客户端无法修改它的值。 As it is just one object, when you update _someData
, someData
is also updated.因为它只是一个对象,所以当您更新
_someData
时, someData
也会更新。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.