Room : LiveData from Dao will trigger Observer.onChanged on every Update, even if the LiveData value has no change

There is simple solution in Transformations method distinctUntilChanged.expose new data only if data was changed. In this case we get data only when it changes in source: LiveData<YourType> getData(){ return Transformations.distinctUntilChanged(LiveData<YourType> source)); } But for Event cases is better to use this: https://stackoverflow.com/a/55212795/9381524

LiveData remove Observer after first callback

There is a more convenient solution for Kotlin with extensions: fun <T> LiveData<T>.observeOnce(lifecycleOwner: LifecycleOwner, observer: Observer<T>) { observe(lifecycleOwner, object : Observer<T> { override fun onChanged(t: T?) { observer.onChanged(t) removeObserver(this) } }) } This extension permit us to do that: liveData.observeOnce(this, Observer<Password> { if (it != null) { // do something } }) So to answer … Read more

Why there’s a separate MutableLiveData subclass of LiveData?

In LiveData – Android Developer Documentation, you can see that for LiveData, setValue() & postValue() methods are not public. Whereas, in MutableLiveData – Android Developer Documentation, you can see that, MutableLiveData extends LiveData internally and also the two magic methods of LiveData is publicly available in this and they are setValue() & postValue(). setValue(): set … Read more

Room – LiveData observer does not trigger when database is updated

I had a similar problem using Dagger 2 that was caused by having different instances of the Dao, one for updating/inserting data, and a different instance providing the LiveData for observing. Once I configured Dagger to manage a singleton instance of the Dao, then I could insert data in the background (in my case in … Read more

LiveData prevent receive the last value when start observing

I`m using this EventWraper class from Google Samples inside MutableLiveData /** * Used as a wrapper for data that is exposed via a LiveData that represents an event. */ public class Event<T> { private T mContent; private boolean hasBeenHandled = false; public Event( T content) { if (content == null) { throw new IllegalArgumentException(“null values … Read more

Notify Observer when item is added to List of LiveData

Internally, LiveData keeps track of each change as a version number (simple counter stored as an int). Calling setValue() increments this version and updates any observers with the new data (only if the observer’s version number is less than the LiveData’s version). It appears the only way to start this process is by calling setValue() … Read more