Class Transformations
LiveData.
These methods permit functional composition and delegation of LiveData instances. The
transformations are calculated lazily, and will run only when the returned LiveData is
observed. Lifecycle behavior is propagated from the input source LiveData to the
returned one.
-
Method Summary
Modifier and TypeMethodDescriptionstatic <X> LiveData<X> distinctUntilChanged(LiveData<X> source) Creates a newLiveDataobject that does not emit a value until the source LiveData value has been changed.static <X,Y> LiveData <Y> Returns aLiveDatamapped from the inputsourceLiveDataby applyingmapFunctionto each value set onsource.static <X,Y> LiveData <Y> switchMap(LiveData<X> source, Function<? super X, ? extends LiveData<? extends Y>> switchMapFunction) Returns aLiveDatamapped from the inputsourceLiveDataby applyingswitchMapFunctionto each value set onsource.
-
Method Details
-
map
@UiThread @Nonnull public static <X,Y> LiveData<Y> map(@Nonnull LiveData<X> source, @Nonnull Function<? super X, ? extends Y> mapFunction) Returns aLiveDatamapped from the inputsourceLiveDataby applyingmapFunctionto each value set onsource.This method is analogous to
.invalid reference
io.reactivex.rxjava3.core.Observable#maptransformwill be executed on the UI thread.Here is an example mapping a simple
Userstruct in aLiveDatato aLiveDatacontaining their full name as aString.LiveData<User> userLiveData = ...; LiveData<String> userFullNameLiveData = Transformations.map( userLiveData, user -> user.firstName + user.lastName);- Type Parameters:
X- the generic type parameter ofsourceY- the generic type parameter of the returnedLiveData- Parameters:
source- theLiveDatato map frommapFunction- a function to apply to each value set onsourcein order to set it on the outputLiveData- Returns:
- a LiveData mapped from
sourceto type<Y>by applyingmapFunctionto each value set.
-
switchMap
@UiThread @Nonnull public static <X,Y> LiveData<Y> switchMap(@Nonnull LiveData<X> source, @Nonnull Function<? super X, ? extends LiveData<? extends Y>> switchMapFunction) Returns aLiveDatamapped from the inputsourceLiveDataby applyingswitchMapFunctionto each value set onsource.The returned
LiveDatadelegates to the most recentLiveDatacreated by callingswitchMapFunctionwith the most recent value set tosource, without changing the reference. In this way,switchMapFunctioncan change the 'backing'LiveDatatransparently to any observer registered to theLiveDatareturned byswitchMap().Note that when the backing
LiveDatais switched, no further values from the olderLiveDatawill be set to the outputLiveData. In this way, the method is analogous to.invalid reference
io.reactivex.rxjava3.core.Observable#switchMapswitchMapFunctionwill be executed on the UI thread.Here is an example class that holds a typed-in name of a user
String(such as from anEditText) in aMutableLiveDataand returns aLiveDatacontaining a List ofUserobjects for users that have that name. It populates thatLiveDataby re-querying a repository-pattern object each time the typed name changes.This
ViewModelwould permit the observing UI to update "live" as the user ID text changes.class UserViewModel extends ViewModel { MutableLiveData<String> nameQueryLiveData = ... LiveData<List<String>> getUsersWithNameLiveData() { return Transformations.switchMap( nameQueryLiveData, myDataSource::getUsersWithNameLiveData); } void setNameQuery(String name) { this.nameQueryLiveData.setValue(name); } }- Type Parameters:
X- the generic type parameter ofsourceY- the generic type parameter of the returnedLiveData- Parameters:
source- theLiveDatato map fromswitchMapFunction- a function to apply to each value set onsourceto create a new delegateLiveDatafor the returned one- Returns:
- a LiveData mapped from
sourceto type<Y>by delegating to the LiveData returned by applyingswitchMapFunctionto each value set
-
distinctUntilChanged
Creates a newLiveDataobject that does not emit a value until the source LiveData value has been changed. The value is considered changed ifObject.equals(Object)yieldsfalse.
-