Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ObservableField or LiveData? Which one is the best?

I have been testing Livedata and AAC in general.

What is the core difference between LiveData and ObservableField?

Which one is the best and when should I use one over another?

like image 865
Rick Prata Avatar asked Jul 20 '18 06:07

Rick Prata


People also ask

How LiveData is different from ObservableField?

ObservableField is not Lifecycle aware but LiveData is lifecycle aware and hence will notify only the observables which are “active”.

What is the advantage of implementing LiveData and data binding?

The advantages of using LiveData Using LiveData provides the following advantages: Ensures your UI matches your data state. LiveData follows the observer pattern. LiveData notifies Observer objects when underlying data changes.

What is LiveData and 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).

In which method should you observe a LiveData object?

Else, we can use callback or RxJava in ViewModel. Another compromise is implement MediatorLiveData (or Transformations) and observe (put your logic here) in ViewModel. Notice MediatorLiveData observer won't trigger (same as Transformations) unless it's observed in Activity/Fragment.


1 Answers

The core difference is that ObservableField<T> is not lifecycle-aware and hence there cannot be any automatic subscription management. While LiveData<T> is lifecycle-aware and solves tons of headaches with subscription management when it comes to Activity/Fragment lifecycles.

There is no one way to answer what is the best to use. It is a personal choice, but I would suggest using LiveData<T> just to save yourself some time and avoid potential issues in the future.

like image 97
Tomas Jablonskis Avatar answered Sep 23 '22 10:09

Tomas Jablonskis