Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

LiveData or DataBinding Observer

I using MVVM on my Android application, on ViewModel I have many observers (from data binding) like ObservableBoolean, ObservableField, I read that I can use LiveData/MutableLiveData instead this observers... What's the difference? I can replace all my data binding observers by LiveData/MutableLiveData?

eg:

replace:

val loading: ObservableBoolean = ObservableBoolean()

By:

val loading: MutableLiveData<Boolean> = MutableLiveData()
like image 914
felipe.rce Avatar asked Oct 16 '18 00:10

felipe.rce


People also ask

What is difference between observable and LiveData?

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.

When should I use DataBinding?

Data Binding allows you to effortlessly communicate across views and data sources. This pattern is important for many Android designs, including model view ViewModel (MVVM), which is currently one of the most common Android architecture patterns.

Which is better ViewBinding and DataBinding?

ViewBinding vs DataBindingThe main advantages of viewbinding are speed and efficiency. It has a shorter build time because it avoids the overhead and performance issues associated with DataBinding due to annotation processors affecting DataBinding's build time.


1 Answers

Many times has passed and I learned a lot... Replace all Data Binding Observable by LiveData, because LiveData respect the Activity lifecycle and can be used in JetPack lib's, like Room, Coroutine...

like image 122
felipe.rce Avatar answered Oct 28 '22 18:10

felipe.rce