Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between LiveData, MutableLiveData [duplicate]

I know that the MutableLiveData extends LiveData, but what is the difference between them (in usage). What are appropriate use cases, means when to use the correct one from the two?

like image 410
Haitham Haitham Avatar asked Oct 12 '18 05:10

Haitham Haitham


People also ask

What is the difference between MutableLiveData and LiveData?

The only key difference between LiveData and MutableLiveData is that you can change the value of MutableLiveData but LiveData does not allow you to change it's value.

What is difference between LiveData and MutableLiveData in Kotlin?

By using LiveData we can only observe the data and cannot set the data. MutableLiveData is mutable and is a subclass of LiveData. In MutableLiveData we can observe and set the values using postValue() and setValue() methods (the former being thread-safe) so that we can dispatch values to any live or active observers.

Is MutableLiveData thread-safe?

MutableLiveData is LiveData which is mutable & thread-safe. It's not really that LiveData is immutable, just that it can't be modified outside of the ViewModel class. The ViewModel class can modify it however it wants (e.g. a timer ViewModel).

Why use flow instead of LiveData?

StateFlow and LiveData have similarities. Both are observable data holder classes, and both follow a similar pattern when used in your app architecture. The StateFlow and LiveData do behave differently: StateFlow requires an initial state to be passed into the constructor, while LiveData does not.


1 Answers

LiveData is immutable while MutableLiveData is mutable. MutableLiveData extends LiveData and provides methods like setValue() and postValue().

like image 99
theanilpaudel Avatar answered Sep 18 '22 17:09

theanilpaudel