How and where to use Transformations.switchMap?

In recent Android Architecture Components library released by Google, we have two static functions in the Transformations class. While the map function is straight forward and easily understandable, I am finding it hard to properly understand the switchMap function.

The official documentation of switchMap can be found here.

Can somebody explain how and where to use the switchMap function with a practical example?


In the map() function

LiveData userLiveData = ...;
LiveData userName = Transformations.map(userLiveData, user -> {
     return user.firstName + " " + user.lastName; // Returns String
});

everytime the value of userLiveData changes, userName will be updated too. Notice that we are returning a String.

In the switchMap() function:

MutableLiveData userIdLiveData = ...;
LiveData userLiveData = Transformations.switchMap(userIdLiveData, id ->
    repository.getUserById(id)); // Returns LiveData

void setUserId(String userId) {
     this.userIdLiveData.setValue(userId);
}

everytime the value of userIdLiveData changes, repository.getUserById(id) will be called, just like the map function. But repository.getUserById(id) returns a LiveData. So everytime that the value of the LiveData returned by repository.getUserById(id) changes, the value of userLiveData will change too. So the value of userLiveData will depend on changes of userIdLiveData and changes of the value of repository.getUserById(id).

Practical example of switchMap(): imagine you have a user profile with a follow button and a next profile button which sets another profile info. Next profile button will call setUserId() with another id so userLiveData will change and UI will change. Follow button will call the DAO to add one follower more to that user, so the user will have 301 followers instead of 300. userLiveData will have this update that comes from the repository, which comes from the DAO.

How and where to use Transformations.switchMap?, Clean, Easy & New- How To Architect Your App: Part 4 — LiveData Transformations. How to use map() and switchMap() to easily transform the value received  In recent Android Architecture Components library released by Google, we have two static functions in the Transformations class. While the map function is straight forward and easily understandable, I am finding it hard to properly understand the switchMap function. The official documentation of switchMap can be found here. Can somebody explain how and where to use the switchMap function with


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

Clean, Easy & New- How To Architect Your App: Part 4, In this post, I briefly explain with example how to use android LiveData Transformations with map and swicthMap. LiveData Transformations  There are currently two types of Transformations we have. Transformations.map: The map lets you apply a function to the output of LiveData and then propagates the result to the downstream. Transformations.switchMap: The swicthMap function transformation is a lot like a map but for mapping function that emits LiveData instead of values.


For those who want more explanation of @DamiaFuentes switchmap() function example given below:

 MutableLiveData userIdLiveData = ...;
 LiveData userLiveData = Transformations.switchMap(userIdLiveData, id ->
     repository.getUserById(id));

 void setUserId(String userId) {
      this.userIdLiveData.setValue(userId);
 }

In a scenario where the repository contains User(1, "Jane") and User(2, "John"), when the userIdLiveData value is set to "1", the switchMap will call getUser(1), that will return a LiveData containing the value User(1, "Jane"). So now, the userLiveData will emit User(1, "Jane"). When the user in the repository gets updated to User(1, "Sarah"), the userLiveData gets automatically notified and will emit User(1, "Sarah").

When the setUserId method is called with userId = "2", the value of the userIdLiveData changes and automatically triggers a request for getting the user with id "2" from the repository. So, the userLiveData emits User(2, "John"). The LiveData returned by repository.getUserById(1) is removed as a source.

From this example, we can understand that the userIdLiveData is the trigger and the LiveData returned by the repository.getUserById is the "backing" LiveData.

For more reference, check out: https://developer.android.com/reference/android/arch/lifecycle/Transformations

Android LiveData Transformations Example, object so that it is notified of changes. You usually attach the Observer object in a UI controller, such as an activity or fragment. How to use map() and switchMap() to easily transform the value received from LiveData, for elegant code. In the architecture I presented on previous posts (part1, part2, part3) I explained that I like to use a UseCase object which is in charge of the interaction between the data layer and the UI / Presentation layer.


LiveData Overview, <T> LiveData subclass which may observe other LiveData objects and react on OnChanged events from them. This class correctly propagates its active/inactive states down to source LiveData objects. Java Code Examples for android.arch.lifecycle.Transformations.switchMap() The following are Jave code examples for showing how to use switchMap() of the android.arch.lifecycle.Transformations class. You can vote up the examples you like. Your votes will be used in our system to get more good examples.


MediatorLiveData, Transformations.switchMap. Note that when your View is destroyed, you don't need to tear down these subscriptions because the lifecycle of the  Use Transformations.map() to define currentTimeString. Pass in the currentTime and a lambda function to format the time. You can implement the lambda function using the DateUtils.formatElapsedTime() utility method, which takes a long number of milliseconds and formats it to " MM:SS " string format.


LiveData beyond the ViewModel, arch.lifecycle.Transformations.switchMap(). The following are Jave code examples for showing how to use switchMap() of the android  How and where to use Transformations.switchMap? (2) In recent Android Architecture Components library released by Google, we have two static functions in the Transformations class. While the map function is straight forward and easily understandable, I am finding it hard to properly understand the switchMap function.