Use MediatorLiveData in Android

Tram Ho

Introduce:

  • Livedata is a powerful component that has been officially introduced by Google in Android Architecture Components . Those who have used this component realize its usefulness in notifying to views when data changes and it is better to be able to identify the life cycle of its observers to ensure that only the update is called. when they are still active. As an almost indispensable part in a project.
  • Today I would like to introduce to a small component in LiveData that helps us solve difficult problems in a simple way. That is: MediatorLiveData .

Question:

On one screen, we have a ViewModel that contains a LiveData <List <Book>> which is the list of books taken from Room . And our views will listen to this live data to update the UI for it. Simple, and works well.

  • And now, this screen allows the user to change the order of the book list.

  • How do we do this. Currently, the view in this screen only listens to 1 LiveData, and the data that LiveData contains is taken from Room so we cannot control and change it directly. So how can we make the same LiveData emit list book in different order? Remember that we need to make sure that the data displayed of this view is still taken from Room.

Problem solving:

  • And the first way that everyone knows (: v) is to update from the original is to edit the data in the Room as you like and then Room will emit book list to livedata, the view is listening and will update.
  • So what is the problem? If we do n’t want to change the code under Room and re-enforce data integrity. And this is the time to use MediatorLiveData .

MediatorLiveData: LiveData subclass which may observe other LiveData objects and react on OnChanged events from them.

  • I temporarily translate: MediatorLiveData: is the Subclass of Live data, it can listen to onChanged events of other liveData.
  • And so, with MediatorLiveData, we can listen to the list book from Room database and also update its data without changing under Room as follows:

  • And now, the view in the screen above can continue to listen from a data stream, and whenever we call viewModel.rearrangeBooks (), the same stream will generate a new list with a new order.
  • Normally, I would approach MediatorLiveData as a LiveData so its API would not leak to the view – and I didn’t do it here to keep the example simple and straightforward.
  • And in this case we can have 2 different LiveData from BookDao and use MediatorLive to mediate them, here is another way of code:

-> We can see MediatorLiveData can now listen to events from 2 other LiveData . This will make MediatorLiveData look more like a “mediator”.

  • And this is just a simple way to apply for a difficult case. For example, when we want to listen to exceptions, data from different streams … while we’re at the ViewModel and don’t have LifecycleOwner around, or combine multiple LiveData as one. For such cases, MediatorLiveData is exactly what we are looking for.

Epilogue:

  • This article I have introduced to you a way to take advantage of the power of LiveData . This api also has many interesting features now and in the future. We will continue to learn it in the following article.
  • Hope the article is useful to you. Good luck!

Refer:

Share the news now

Source : Viblo