Stay organized with collections Save and categorize content based on your preferences.

Transformations

public class 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 sourceLiveData to the returned one.

Summary

Public constructors

Public methods

static @NonNull LiveData<X>

Creates a new LiveData object that does not emit a value until the source LiveData value has been changed.

static @NonNull LiveData<Y>
@MainThread
<X, Y> map(@NonNull LiveData<X> source, @NonNull Function<X, Y> mapFunction)

Returns a LiveData mapped from the input sourceLiveData by applying mapFunction to each value set on source.

static @NonNull LiveData<Y>
@MainThread
<X, Y> switchMap(
    @NonNull LiveData<X> source,
    @NonNull Function<X, LiveData<Y>> switchMapFunction
)

Returns a LiveData mapped from the input sourceLiveData by applying switchMapFunction to each value set on source.

Public constructors

Transformations

public final Transformations()

Public methods

distinctUntilChanged

@MainThread
public static @NonNull LiveData<X> <X> distinctUntilChanged(@NonNull 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
<X>

the generic type parameter of source

@NonNull LiveData<X> source

the input LiveData

Returns
@NonNull LiveData<X>

a new LiveData of type X

map

@MainThread
public static @NonNull LiveData<Y> <X, Y> map(@NonNull LiveData<X> source, @NonNull Function<X, Y> mapFunction)

Returns a LiveData mapped from the input sourceLiveData by applying mapFunction to each value set on source.

This method is analogous to map.

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<User> userLiveData = ...;
LiveData<String> userFullNameLiveData =
    Transformations.map(
        userLiveData,
        user -> user.firstName + user.lastName);
});
Parameters
<X>

the generic type parameter of source

<Y>

the generic type parameter of the returned LiveData

@NonNull LiveData<X> source

the LiveData to map from

@NonNull Function<X, Y> mapFunction

a function to apply to each value set on source in order to set it on the output LiveData

Returns
@NonNull LiveData<Y>

a LiveData mapped from source to type <Y> by applying mapFunction to each value set.

switchMap

@MainThread
public static @NonNull LiveData<Y> <X, Y> switchMap(
    @NonNull LiveData<X> source,
    @NonNull Function<X, LiveData<Y>> switchMapFunction
)

Returns a LiveData mapped from the input sourceLiveData 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 switchMap.

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<String> nameQueryLiveData = ...

    LiveData<List<String>> getUsersWithNameLiveData() {
        return Transformations.switchMap(
            nameQueryLiveData,
                name -> myDataSource.getUsersWithNameLiveData(name));
    }

    void setNameQuery(String name) {
        this.nameQueryLiveData.setValue(name);
    }
}
Parameters
<X>

the generic type parameter of source

<Y>

the generic type parameter of the returned LiveData

@NonNull LiveData<X> source

the LiveData to map from

@NonNull Function<X, LiveData<Y>> switchMapFunction

a function to apply to each value set on source to create a new delegate LiveData for the returned one

Returns
@NonNull LiveData<Y>

a LiveData mapped from source to type <Y> by delegating to the LiveData returned by applying switchMapFunction to each value set