简体   繁体   English

当在 MutableLiveData 变量上调用 postValue 时,公开的 LiveData 调度如何变化?

[英]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 个不同的变量,如何调用someDataonChanged()方法,即使值发布在_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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM