Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 611
Ehtesham Hasan Avatar asked Dec 02 '17 17:12

Ehtesham Hasan


People also ask

What is transformation switchMap?

switchMap(LiveData<X> trigger, Function<X, LiveData<Y>> func) Creates a LiveData, let's name it swLiveData , which follows next flow: it reacts on changes of trigger LiveData, applies the given function to new value of trigger LiveData and sets resulting LiveData as a "backing" LiveData to swLiveData .

What are the transformation functions available in LiveData package?

Transforming LiveData is pretty easy to do and there is an awesome helper class named Transformations for just this purpose. This class provides three static methods: map , switchMap and distinctUntilChanged which will be explained below.

What is live data transformation?

Zero-Downtime Encryption Deployments CipherTrust Live Data Transformation enables your administrators to encrypt data with zero downtime or any disruption to users, applications, or workflows. While encryption is underway, users and processes can continue to interact with databases or file systems as normal.

When transformations map () method is executed on live data What will it return?

map() will return a LiveData object that should be observed for the mapFunction to be called. Assume a MutableLiveData Int variable that holds a handicap increment value. When this value changes, avgWHDCP for all bowlers in the list needs to be re-computed. Initially it is set to zero.


2 Answers

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.

like image 113
Damia Fuentes Avatar answered Oct 14 '22 05:10

Damia Fuentes


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

like image 22
Prakash Avatar answered Oct 14 '22 03:10

Prakash