Class MediatorLiveData<T>
- Type Parameters:
T- The type of data hold by this instance
LiveData subclass which may observe other LiveData objects and react on
OnChanged events from them.
This class correctly propagates its active/inactive states down to source LiveData
objects.
Consider the following scenario: we have 2 instances of LiveData, let's name them
liveData1 and liveData2, and we want to merge their emissions in one object:
liveDataMerger. Then, liveData1 and liveData2 will become sources for
the MediatorLiveData liveDataMerger and every time onChanged callback
is called for either of them, we set a new value in liveDataMerger.
LiveData<Integer> liveData1 = ...;
LiveData<Integer> liveData2 = ...;
MediatorLiveData<Integer> liveDataMerger = new MediatorLiveData<>();
liveDataMerger.addSource(liveData1, liveDataMerger::setValue);
liveDataMerger.addSource(liveData2, liveDataMerger::setValue);
Let's consider that we only want 10 values emitted by liveData1, to be
merged in the liveDataMerger. Then, after 10 values, we can stop listening to
liveData1 and remove it as a source.
liveDataMerger.addSource(liveData1, new Observer<>() {
private int count = 1;
@Override
public void onChanged(@Nullable Integer s) {
count++;
liveDataMerger.setValue(s);
if (count > 10) {
liveDataMerger.removeSource(liveData1);
}
}
});
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescription<S> voidStarts to listen the givensourceLiveData,onChangedobserver will be called whensourcevalue was changed.protected voidonActive()Called when the number of active observers change to 1 from 0.protected voidCalled when the number of active observers change from 1 to 0.<S> voidremoveSource(LiveData<S> toRemote) Stops to listen the givenLiveData.Methods inherited from class icyllis.modernui.lifecycle.MutableLiveData
postValue, setValueMethods inherited from class icyllis.modernui.lifecycle.LiveData
getValue, hasActiveObservers, hasObservers, observe, observeForever, removeObserver, removeObservers
-
Constructor Details
-
MediatorLiveData
public MediatorLiveData()
-
-
Method Details
-
addSource
@UiThread public <S> void addSource(@Nonnull LiveData<S> source, @Nonnull Observer<? super S> onChanged) Starts to listen the givensourceLiveData,onChangedobserver will be called whensourcevalue was changed.onChangedcallback will be called only when thisMediatorLiveDatais active.If the given LiveData is already added as a source but with a different Observer,
IllegalArgumentExceptionwill be thrown.- Type Parameters:
S- The type of data hold bysourceLiveData- Parameters:
source- theLiveDatato listen toonChanged- The observer that will receive the events
-
removeSource
Stops to listen the givenLiveData.- Type Parameters:
S- the type of data hold bysourceLiveData- Parameters:
toRemote-LiveDatato stop to listen
-
onActive
Description copied from class:LiveDataCalled when the number of active observers change to 1 from 0.This callback can be used to know that this LiveData is being used thus should be kept up to date.
-
onInactive
Description copied from class:LiveDataCalled when the number of active observers change from 1 to 0.This does not mean that there are no observers left, there may still be observers but their lifecycle states aren't
Lifecycle.State.STARTEDorLifecycle.State.RESUMED(like an Activity in the back stack).You can check if there are observers via
LiveData.hasObservers().- Overrides:
onInactivein classLiveData<T>
-