Open In App

LiveData vs ObservableField in Android

LiveData helps to reduce memory leaks and manual lifecycle handling. It also ensures that UI is always up to date with the data even when the app’s activity is restarted while we are still using our app. It is a part of the architecture patterns of android. It is used for observing changes in the view and updating the view when it is ACTIVE. Thus, LiveData is lifecycle aware.

An observable class that holds a parcelable object (allows objects to read and write from Parcels which can contain flattened data inside message containers). An object wrapper to make it observable. ObservableField classes may be used instead of creating an Observable object. If your object class has fewer properties to be updated or if you don’t want to observe every field in the object, you can use ObservableFields to update the UI. You can declare the variable as ObservableField and when the new data is set, the UI will be updated.



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. There are situations where you might prefer to use a View Model component that implements the Observable interface over using LiveData objects, even if you lose the lifecycle management capabilities of LiveData. Using a View Model 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.

Similarities

Differences

When to Use What?

Now, let’s understand ObservableField and why LiveData was required even when ObservableField was present.



ViewModel + LiveData + Data Binding = Reactive UI




// LiveData
  
class SampleViewModel : ViewModel() {
  
    // Create a LiveData with a String
    val currentName : MutableLiveData<String> by lazy
    {
        MutableLiveData<String>()
    }
  
    // Rest of the ViewModel...
}
  
// Declaring it
val liveDataA = MutableLiveData<String>()
                // Trigger the value change
                liveDataA.value
    = someValue
    // Optionally, one could use liveDataA.postValue(value)
    // to get it set on the UI thread

What are the Benefits of Using LiveData?

You can use LiveData all the time, as long as there is a Lifecycle Owner to observe. I prefer to keep bound fields that are only relevant to the ViewModel as Observable and use LiveData for fields whose state changes are also relevant to the Activity or Fragment.




// ObservableField
  
public class MyDataObject {
    private Context context;
    public final ObservableField<String> A
        = new ObservableField<String>();
    public final ObservableField<String> B
        = new ObservableField<String>();
    public final ObservableField<String> display
        = new ObservableField<String>(A, B) {
              @Override public String get()
              {
                  return context.getResources().getString(
                      R.string.name, first.get, last.get());
              }
          };
    public final ObservableInt age = new ObservableInt();
}


Article Tags :