Reputation: 611
I have a question about MutableLiveData
in Viewmodel
. Does just setValue
function of MutableLiveData
trigger observation? If we change content of MutableLiveData
witout setValue
, may it be triggered?
Upvotes: 3
Views: 2154
Reputation: 5770
It will only trigger if you call setValue
or postValue
. If you use Kotlin then you can write yourself an extension to trigger the LiveData
:
fun <T> MutableLiveData<T>.trigger() {
value = value
}
and then you can simply call:
mutableLiveData.trigger()
Upvotes: 1
Reputation: 171
Both setValue()
& postValue
will trigger the events. The only difference is, postValue()
can trigger the observation event from background thread as well. Whereas, setValue
must be called within main thread.
postValue()
is preferred to setValue()
.
Upvotes: 0
Reputation: 1474
I doubt it. Only the mothods below dispatch events to observables:
liveData.postValue("a");
liveData.setValue("b");
https://developer.android.com/reference/android/arch/lifecycle/MutableLiveData
Upvotes: 1