Ali Rezaiyan
Ali Rezaiyan

Reputation: 3309

LiveData vs Observable data object

According to the LiveData documentation, one of the features has been designed to notify data changes using observable pattern. (Along with this,LiveData offers a number of other promising features)

On the other hand, the Observable data object based on its documentation is capable of notifying others about changes in its data.

As a result, these two features seem are same.

The question is:

isn't it better to use LiveData with its other features?

Upvotes: 4

Views: 5591

Answers (3)

Keivan Esbati
Keivan Esbati

Reputation: 3454

According to LiveData documentation:

LiveData is an observable data holder class. Unlike a regular observable, LiveData is lifecycle-aware, meaning it respects the lifecycle of other app components, such as activities, fragments, or services. This awareness ensures LiveData only updates app component observers that are in an active lifecycle state.

So it is essentially a lifecycle-aware observable, isn't it? Contrary to common belief, there is really no restriction about using only RxJava or LiveData, the two can be used along each other.

For example, it better to use RxJava where there is no view lifecycle available like Repository in the data layer, and keep LiveData in layers that view lifecycle is available like Presentation layer.

2025 Update: It is a misconception that RxJava (or observers in general) don't have lifecycles. You see, in programming every object has a lifecycle, otherwise you are going to face memory leaks, the only difference here is that LiveData take View lifecycle as its own, making it easier to avoid leaks.

Upvotes: 6

Weidian Huang
Weidian Huang

Reputation: 2945

The observable in the question is ObservableFields, not RxJava Obervervable like the other answers mentioned.

It's a good question to compare between LiveData and ObservableFields. Because live data is lifecycle aware, it can do a lot of things. You can observe the live data from the view model and update the UI from the code. You can also extend the live data and use it like an event bus to notify the data change between 2 Android components and you don't need to worry about the lifecycle issue. But does that mean it is not necessary to use ObservableFields at all? No. Because data binding itself is lifecycle aware, it already checks when the view is active. You can use LiveData or ObservableFields or even Stateflow

In one-way data binding, you can use any of them, but in 2-way data binding, I still prefer observable fields, because it's more flexible, and you can easily write custom logic in the observable field setter, and when the UI changes, it can call the setter to do some additional stuff.

class LoginViewModel : BaseObservable {
    // val data = ...

    @Bindable
    fun getRememberMe(): Boolean {
        return data.rememberMe
    }

    fun setRememberMe(value: Boolean) {
        // Avoids infinite loops.
        if (data.rememberMe != value) {
            data.rememberMe = value

            // React to the change.
            saveData()

            // Notify observers of a new value.
            notifyPropertyChanged(BR.remember_me)
        }
    }
}

And in xml file.

<CheckBox
    android:id="@+id/rememberMeCheckBox"
    android:checked="@={viewmodel.rememberMe}"
/>

Upvotes: 1

AvidRP
AvidRP

Reputation: 383

LiveData is like an observable but like unlike an observable, it is lifecycle aware. So this means that the live data will only update app component observers that are in an active state. However, you don't always need to use livedata. I would say livedata comes in handy when there are lifecycle components involved such as activities and fragments. For more information look at When to use RxJava in Android and when to use LiveData from Android Architectural Components?

Upvotes: 2

Related Questions