Observer being able to distinguish between new and "cached" data
See original GitHub issueFirst off, the concept of having LiveData (and MutableLiveData) with observers that handle Lifecycle is a great addition on top of the bare metal Android components.
Having played around with LiveData and Observer there arrived one case where it would really be desirable to know if LiveData is passing on a cached version (mVersion) due to the change of LifecycleBoundObserver (initiator), this references the filed/param names in LiveData.java.
Following here is a case where it would be good to know if LiveData is cached or not:
It seems to be a good pattern to create LiveData that is private final and associate a getter with it, looks usually something like below:
private final MutableLiveData<ApiResponse<DownloadResponse>> mDownloadLiveData = new MutableLiveData<>();
A Fragment would then register an Observer on that LiveData
mViewModel.getDownloadLiveData().observe(this, mDownloadObserver);
mDownloadObserver in this case would also be private final inside Fragment
private final mDownloadObserver = new Observer ...
So far so good, the Code will notify the Observer when new data arrives and consider the applications Lifecycle.
However, as soon as the Fragment undergoes a configuration change, LiveData realizes that LifecycleBoundObserver has changed and mVersion does not match observer.lastVersion (in fact it has been initialized to -1) and thus publishes it’s underlying data mData with onChanged.
I am certain this is how it is intended, and indeed it seems that most applications expect this behavior. However in certain cases it would be desirable to know that the onChange has been fired with LifecycleBoundObserver.lastVersion = -1.
Of course placing a state variable in the Fragment would allow for checking if the Data has been manually requested, yet it seems to negate the purpose of the stateless design around the Architecture components.
A suggestion would be to have a second option that allows to register a stateful Observer knowing if the callback is issued due to a new LifeCycleBoundObserver.
public void observe(LifecycleOwner owner, StatefulObserver<T> observer)
and
public interface StatefulObserver<T> {
/**
* Called when the data is changed.
* @param t The new data
* @param fromCache whether or not the value is submited due to a new LifeCycleBoundObserver
*/
void onChanged(@Nullable T t, boolean fromCache);
}
Again thanks a lot and keep up the good work! Manuel
Issue Analytics
- State:
- Created 6 years ago
- Reactions:6
- Comments:5 (2 by maintainers)

Top Related StackOverflow Question
You can check the
SingleEventclass in this example: https://github.com/googlesamples/android-architecture/tree/dev-todo-mvvm-live We are thinking about moving it to the library.@yigit
SingleEventworks fine but will it create any problems if I also addSo if it is being used as a source to another
MediatorLiveDatait does not send events toonChanged()of the observer when orientation is changed.