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 newLiveData
object that does not emit a value until the source LiveData value has been changed.static <X,
Y> LiveData <Y> Returns aLiveData
mapped from the inputsource
LiveData
by applyingmapFunction
to each value set onsource
.static <X,
Y> LiveData <Y> switchMap
(LiveData<X> source, Function<? super X, ? extends LiveData<? extends Y>> switchMapFunction) Returns aLiveData
mapped from the inputsource
LiveData
by applyingswitchMapFunction
to 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 aLiveData
mapped from the inputsource
LiveData
by applyingmapFunction
to each value set onsource
.This method is analogous to
invalid reference
io.reactivex.rxjava3.core.Observable#map
transform
will be executed on the UI thread.Here is an example mapping a simple
User
struct in aLiveData
to aLiveData
containing 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 ofsource
Y
- the generic type parameter of the returnedLiveData
- Parameters:
source
- theLiveData
to map frommapFunction
- a function to apply to each value set onsource
in order to set it on the outputLiveData
- Returns:
- a LiveData mapped from
source
to type<Y>
by applyingmapFunction
to 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 aLiveData
mapped from the inputsource
LiveData
by applyingswitchMapFunction
to each value set onsource
.The returned
LiveData
delegates to the most recentLiveData
created by callingswitchMapFunction
with the most recent value set tosource
, without changing the reference. In this way,switchMapFunction
can change the 'backing'LiveData
transparently to any observer registered to theLiveData
returned byswitchMap()
.Note that when the backing
LiveData
is switched, no further values from the olderLiveData
will be set to the outputLiveData
. In this way, the method is analogous toinvalid reference
io.reactivex.rxjava3.core.Observable#switchMap
switchMapFunction
will 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 aMutableLiveData
and returns aLiveData
containing a List ofUser
objects for users that have that name. It populates thatLiveData
by re-querying a repository-pattern object each time the typed name changes.This
ViewModel
would 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 ofsource
Y
- the generic type parameter of the returnedLiveData
- Parameters:
source
- theLiveData
to map fromswitchMapFunction
- a function to apply to each value set onsource
to create a new delegateLiveData
for the returned one- Returns:
- a LiveData mapped from
source
to type<Y>
by delegating to the LiveData returned by applyingswitchMapFunction
to each value set
-
distinctUntilChanged
Creates a newLiveData
object that does not emit a value until the source LiveData value has been changed. The value is considered changed ifObject.equals(Object)
yieldsfalse
.
-