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
-
Method Summary
Modifier and TypeMethodDescription<S> void
Starts to listen the givensource
LiveData,onChanged
observer will be called whensource
value was changed.protected void
onActive()
Called when the number of active observers change to 1 from 0.protected void
Called when the number of active observers change from 1 to 0.<S> void
removeSource
(LiveData<S> toRemote) Stops to listen the givenLiveData
.Methods inherited from class icyllis.modernui.lifecycle.MutableLiveData
postValue, setValue
Methods 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 givensource
LiveData,onChanged
observer will be called whensource
value was changed.onChanged
callback will be called only when thisMediatorLiveData
is active.If the given LiveData is already added as a source but with a different Observer,
IllegalArgumentException
will be thrown.- Type Parameters:
S
- The type of data hold bysource
LiveData- Parameters:
source
- theLiveData
to listen toonChanged
- The observer that will receive the events
-
removeSource
Stops to listen the givenLiveData
.- Type Parameters:
S
- the type of data hold bysource
LiveData- Parameters:
toRemote
-LiveData
to stop to listen
-
onActive
Description copied from class:LiveData
Called 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:LiveData
Called 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.STARTED
orLifecycle.State.RESUMED
(like an Activity in the back stack).You can check if there are observers via
LiveData.hasObservers()
.- Overrides:
onInactive
in classLiveData<T>
-