When to use Android’s LiveData and Observable field?

我的未来我决定 提交于 2020-05-10 15:17:12

问题


I’m implementing a MVVM and data-binding and I’m trying to understand when should I use Observable field over LiveData?

I already run through different documentations and discovered that LiveData is lifecycle aware, but in sample codes in Github these two are being used in ViewModel at the same time. So, I’m confused if LiveData is better than Observable field, why not just use LiveData at all?


回答1:


Both have their use-cases, for instance:

  • If you want a life-cycle tolerant container for your UI state model, LiveData is the answer.

  • If you want to make the UI update itself when a piece of logic is changed in your view model, then use ObservableFields.

I myself prefer using a combination of LivaData and ObservableField/BaseObservable, the LiveData will normally behave as a life-cycle aware data container and also a channel between the VM and the View.

On the other hand the UI state model objects that are emitted through the LiveData are themselves BaseObservable or have their fields as ObservableField.

That way I can use the LiveData for total changes of the UI state. And set values to the UI state model ObservableField fields whenever a small portion of the UI is to be updated.

Edit: Here is a quick illustration on a UserProfile component for example:

UIStateModel

data class ProfileUIModel(
    private val _name: String,
    private val _age: Int
): BaseObservable() {
    var name: String
        @Bindable get() = _name
        set(value) {
          _name = value
          notifyPropertyChanged(BR.name)
        }
    var age: Int
        @Bindable get() = _age
        set(value) {
          _age = value
          notifyPropertyChanged(BR.age)
        }
}

ViewModel

class UserProfileViewModel: ViewModel() {

    val profileLiveData: MutableLiveData = MutableLiveData()

    ...

    // When you need to rebind the whole profile UI object.
    profileLiveData.setValue(profileUIModel)

    ...

    // When you need to update a specific part of the UI.
    // This will trigger the notifyPropertyChanged method on the bindable field "age" and hence notify the UI elements that are observing it to update.
    profileLiveData.getValue().age = 20 
}

View

You'll observe the profile LiveData changes normally.

XML

You'll use databinding to bind the UI state model.

Edit: Now the mature me prefers Immutability instead of having mutable properties as explained in the answer.




回答2:


You can use LiveData all the time, as long as there is a LifecycleOwner 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.




回答3:


LiveData - use with LifecycleOwner like activity or fragment

Observable - use with data binding



来源:https://stackoverflow.com/questions/52394858/when-to-use-android-s-livedata-and-observable-field

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!