Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is difference between MediatorLiveData and MutableLiveData in MVVM

I have searched a lot but not found the crystal clear answer for the questions:

  1. What is the difference between MediatorLiveData and MutableLiveData?

  2. What are the suitable condition to use either of them.

like image 860
Lalit Kushwah Avatar asked Oct 25 '17 10:10

Lalit Kushwah


People also ask

What is the difference between MediatorLiveData and MutableLiveData?

MediatorLiveData is a subclass of MutableLiveData made for the situation where you want to observe changes parallel from Multiple LiveData instances. This scenario is by far explained best in documentation here developer.android.com/reference/android/arch/lifecycle/…

What is MediatorLiveData?

android.arch.lifecycle.MediatorLiveData<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.

What is the use of MutableLiveData?

MutableLiveData is commonly used since it provides the postValue() , setValue() methods publicly, something that LiveData class doesn't provide. LiveData/MutableLiveData is commonly used in updating data in a RecyclerView from a collection type(List, ArrayList etc).

What is the difference between mutable and immutable live data?

There are subclasses in LiveData that are useful for their properties when updating the UI. LiveData is immutable by default. By using LiveData we can only observe the data and cannot set the data. MutableLiveData is mutable and is a subclass of LiveData.


1 Answers

At first we need to know what is the relation between MutableLivedata and MediatorLivedata to understand the difference between them.

java.lang.Object   ↳ android.arch.lifecycle.LiveData<T>       ↳ android.arch.lifecycle.MutableLiveData<T>           ↳ android.arch.lifecycle.MediatorLiveData<T> 

Now it is clear that MediatorLiveData is a subclass of MutableLiveData therefore MediatorLiveData can access each and every property of MutableLiveData as well as LiveData.

Question no. 1 is answered partially and rest of the answer will be discussed at the end of Question no. 2's answer.

After researching on some sample projects as well as android developer's official site I found that MutableLiveData should be used only for notifying your UI when observing any data.

For example, you want to display two SeekBars on two different fragments(Fragment1 and Fragment2) and you also want them to be synced when operating from Fragment1.

Another scenario is that we have 2 instances of LiveData, let's name them liveData1 and liveData2, and we want to merge their emissions in one object: liveDataMerger (which is a MediatorLiveData object). Then, liveData1 and liveData2 will become sources for the liveDataMerger and every time onChanged callback is called for either of them, we set a new value in liveDataMerger.

LiveData liveData1 = ...; LiveData liveData2 = ...;  MediatorLiveData liveDataMerger = new MediatorLiveData<>(); liveDataMerger.addSource(liveData1, value ->liveDataMerger.setValue(value)); liveDataMerger.addSource(liveData2, value -> liveDataMerger.setValue(value)); 

In this case you cannot use MutableLiveData but on the other hand if you want to compare data into the first example (where MutableLiveData has been used) then you cannot because you will be unable to use the addSource property (as per class hierarchy).

like image 83
Asad Avatar answered Oct 05 '22 10:10

Asad