In recent Android Architecture Components library released by Google, we have two static functions in the Transformations
class. While the map
function
Adding my 2 cents to @DamiaFuentes answer.
MutableLiveData userIdLiveData = ...;
LiveData userLiveData = Transformations.switchMap(userIdLiveData, id ->
repository.getUserById(id)); // Returns LiveData
void setUserId(String userId) {
this.userIdLiveData.setValue(userId);
}
Transformations.switchMap method will only be called when you have at least one observer for userLiveData