Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

LiveData vs. ObservableField for data binding

There are multiple questions on Stackoverflow related to difference between LiveData and ObservableField. Also, I have found multiple articles on the internet about this topic. All of them explain that LiveData is lifecycle-aware unlike ObservableField. Most of them also mention that it is advantageous to use LiveData instead of ObservableField if component such as Activity or Fragment observe the attribute, so we don't need to unsubscribe.

However, even after reading all of that, what is still not clear to me is if there is or isn't some advantage of using LiveData over ObservableField for data binding. For example:

ViewModel:

class UserViewModel(user: User) : ViewModel {
    val userName = ObservableField<String>(user.name) // Option 1
    val userName = MutableLiveData<String>(user.name) // Option 2
}

Layout:

<layout>
    <data>
        <variable name="viewModel" type="com.example.UserViewModel" />
    </data>
    ...
</layout>

For option 2, I will also have to use binding.setLifecycleOwner(activity), of course. Let's assume that nothing else than the layout observes userName.

My questions is:

Is there any advantage of using option 2 over option 1 or it doesn't matter in this case since the view (layout) will simply observe until it exists ?

What confuses me even more is this article: https://android.jlelse.eu/android-architecture-components-livedata-with-data-binding-7bf85871bbd8 which says: "In the previous approach (without LiveData) if we wanted to show the data on the UI, we should previously check if it still exists. With LiveData we don’t need to worry about it because data will be posted only if Activity is at least started (so in started or resumed state)."

I don't understand this quoted part. What is meant by checking if UI still exists in case of "previous approach" which uses ObservableField? How would you apply this check for my example in option 1 ?

like image 696
1daemon1 Avatar asked Dec 18 '18 15:12

1daemon1


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.

Is LiveData deprecated?

This function is deprecated. The observer will only receive events if the owner is in Lifecycle. State. STARTED or Lifecycle.

What is difference between LiveData and MutableLiveData?

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.


1 Answers

When you use the ObservableField and you listen to its changes then you get notified in both cases:

  1. When activity is visible to a user.
  2. When activity is not visible (in the paused or destroying state).

now when u get notified for such event and you need to change UI like textView etc case 1 will work fine as the activity is visible but in case 2 it is not suggested to change UI as the activity is not visible and also it may crash if view does not exist(Like state of finishing the activity).

so Here to avoid this case 2 crashes and unwanted data changes Live data comes to play as in Live data you will get change events only if an activity is visible. Live data observers works according to the lifecycle of Activity/Fragment. So you don't need to check if view exists before UI update as the observer it itself not called.

like image 151
saksham Avatar answered Sep 23 '22 08:09

saksham