ObservableField or LiveData? Which one is the best?
Both LiveData
and Observable
could be used alternatively. LiveData
is lifecycle aware and hence will notify only the observables which are "active".
Quoting official documentation on https://developer.android.com/topic/libraries/data-binding/architecture#livedata
Unlike objects that implement Observable—such as observable fields—LiveData objects know about the lifecycle of the observers subscribed to the data changes. This knowledge enables many benefits, which are explained in The advantages of using LiveData. In Android Studio version 3.1 and higher, you can replace observable fields with LiveData objects in your data binding code.
As mentioned both are usable for UI binding interchangeably. LiveData
is a quick method but if you want more control on the binding Obserable
is a way to go with.
Quoting official documentation on https://developer.android.com/topic/libraries/data-binding/architecture#observable-viewmodel
There are situations where you might prefer to use a ViewModel component that implements the Observable interface over using LiveData objects, even if you lose the lifecycle management capabilities of LiveData. Using a ViewModel component that implements Observable gives you more control over the binding adapters in your app. For example, this pattern gives you more control over the notifications when data changes, it also allows you to specify a custom method to set the value of an attribute in two-way data binding.
We get options to customize the binding in case of an Observable
which might be helpful in some case.
Personal preference is to go with LiveData
. In case some some customization or more control is needed on the binding go for Obervable
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.
Similarities
- Work well with data binding
- Bound views automatically unsubscribe when in the background
- POJO classes can also subscribe to changes
Differences
LiveData
allows for POJO subscribers to be lifecycle aware. Meaning that if you have a property B that you want to update when A changes, you can opt to not receive updates when the attached view is inactive. This saves resources.- Background thread updates of
ObservableField<T>
are immediate.MutableLiveData.postData
is delayed. - Values of
LiveData<T>
andObservableField<T>
are always nullable, but the primitive implementationsObservableInt
,-Float
,-Boolean
etc are non-nullable. MutableLiveData<T>
doesn't take a constructor value to set on initialization.
When to use what
- Do you have external events that may trigger cascading data change in the ViewModel when the app is in the backgrond? You should probably go for
LiveData
- Do you require value update on background threads to be immediate*? Use Observables
None of the above requirements? You can't go wrong with either but ObservableField
is a simpler class.
*) "Immediate" in the sense that a get
directly after a set/postValue
is guaranteed to return the value you just set. None of the types are of course immediate when it comes to UI update.