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, Y> 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.

static <X, Y> 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.

Inherited methods

Public methods

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).

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.

 LiveData userLiveData = ...;
 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>).

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