ahmetvefa53
ahmetvefa53

Reputation: 611

MutableLivedata observer triggering

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

Answers (3)

Paul Spiesberger
Paul Spiesberger

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

Ningan
Ningan

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

Jeremi
Jeremi

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

Related Questions