Transformations
public
class
Transformations
extends Object
| java.lang.Object | |
| ↳ | androidx.lifecycle.Transformations |
Transformation methods for 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.
Summary
Public methods | |
|---|---|
static
<X>
LiveData<X>
|
distinctUntilChanged(LiveData<X> source)
Creates a new |
static
<X, Y>
LiveData<Y>
|
map(LiveData<X> source, Function<X, Y> mapFunction)
Returns a |
static
<X, Y>
LiveData<Y>
|
switchMap(LiveData<X> source, Function<X, LiveData<Y>> switchMapFunction)
Returns a |
Inherited methods | |
|---|---|
Public methods
distinctUntilChanged
public static LiveData<X> distinctUntilChanged (LiveData<X> source)
Creates a new LiveData object that does not emit a value until the source LiveData
value has been changed. The value is considered changed if equals() yields
false.
| Parameters | |
|---|---|
source |
LiveData: the input LiveData |
| Returns | |
|---|---|
LiveData<X> |
a new LiveData of type X
|
map
public static LiveData<Y> map (LiveData<X> source, Function<X, Y> mapFunction)
Returns a LiveData mapped from the input source LiveData by applying
mapFunction to each value set on source.
This method is analogous to Observable.map(Function super T, ? extends R>).
transform will be executed on the main thread.
Here is an example mapping a simple User struct in a LiveData to a
LiveData containing their full name as a String.
LiveDatauserLiveData = ...; LiveData userFullNameLiveData = Transformations.map( userLiveData, user -> user.firstName + user.lastName); });
| Parameters | |
|---|---|
source |
LiveData: the LiveData to map from |
mapFunction |
Function: a function to apply to each value set on source in order to set
it
on the output LiveData |
| Returns | |
|---|---|
LiveData<Y> |
a LiveData mapped from source to type <Y> by applying
mapFunction to each value set.
|
switchMap
public static LiveData<Y> switchMap (LiveData<X> source, Function<X, LiveData<Y>> switchMapFunction)
Returns a LiveData mapped from the input source LiveData by applying
switchMapFunction to each value set on source.
The returned LiveData delegates to the most recent LiveData created by
calling switchMapFunction with the most recent value set to source, without
changing the reference. In this way, switchMapFunction can change the 'backing'
LiveData transparently to any observer registered to the LiveData returned
by switchMap().
Note that when the backing LiveData is switched, no further values from the older
LiveData will be set to the output LiveData. In this way, the method is
analogous to Observable.switchMap(Function super T, ? extends ObservableSource extends R>>).
switchMapFunction will be executed on the main thread.
Here is an example class that holds a typed-in name of a user
String (such as from an EditText) in a MutableLiveData and
returns a LiveData containing a List of User objects for users that have
that name. It populates that LiveData by requerying 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 AndroidViewModel {
MutableLiveData nameQueryLiveData = ...
LiveData> getUsersWithNameLiveData() {
return Transformations.switchMap(
nameQueryLiveData,
name -> myDataSource.getUsersWithNameLiveData(name));
}
void setNameQuery(String name) {
this.nameQueryLiveData.setValue(name);
}
}
| Parameters | |
|---|---|
source |
LiveData: the LiveData to map from |
switchMapFunction |
Function: a function to apply to each value set on source to create a
new delegate LiveData for the returned one |
| Returns | |
|---|---|
LiveData<Y> |
a LiveData mapped from source to type <Y> by delegating
to the LiveData returned by applying switchMapFunction to each
value set
|