Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Different between ViewModel with LiveData vs ViewModel Without LiveData?

I read many tutorial about viewmodel and livedata but i do not get actually use of livedata within viewmodel class in mvvm pattern. thanks in advance.

like image 522
Hardik Prajapati Avatar asked Feb 23 '19 12:02

Hardik Prajapati


People also ask

What is the difference between ViewModel and LiveData?

ViewModel : Provides data to the UI and acts as a communication center between the Repository and the UI. Hides the backend from the UI. ViewModel instances survive device configuration changes. LiveData : A data holder class that follows the observer pattern, which means that it can be observed.

Can we use LiveData without ViewModel?

To answer your question, No, It is not mandatory to use LiveData always inside ViewModel, it is just an observable pattern to inform the caller about updates in data. If you have something which won't be changed frequently and can be accessed by its instance.

What is the purpose of using LiveData?

LiveData notifies Observer objects when underlying data changes. You can consolidate your code to update the UI in these Observer objects. That way, you don't need to update the UI every time the app data changes because the observer does it for you. No memory leaks.

What is MVVM and LiveData?

Up until now, we've used Data Binding to update the View from the ViewModel. LiveData is a handy data holder that acts as a container over the data to be passed. The best thing about LiveData is that it is lifecycle aware. So if you are in the background, the UI won't try to update.


3 Answers

Android dev commonly use ViewModel as a container of LiveData like this

class MyViewModel {
    val myLiveData = MutableLiveData<String>()
}

But, why don't we use the String class directly?

class MyViewModel {
    val myString = "Hello"
}

Because we want to use Observer pattern to the String.

model.myLiveData.postValue("Echo")

model.myLiveData.observe(this, Observer {
    // Show "Echo"
    toast(it)
})

Why do we need ViewModel then? Because we want to get the same ViewModel instance (singleton) on our Activity or Fragment.

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    viewModel = ViewModelProviders.of(this)[MyViewModel::class.java]
    viewModel.myLiveData.observe(this, Observer {
        textView.text = it
    })

I think that should be enough to start seeing the difference between ViewModel and LiveData.

Different between ViewModel with LiveData vs ViewModel Without LiveData?

That's up to you, do you need a LiveData on a ViewModel or not?

i do not get actually use of livedata within viewmodel class in mvvm pattern.

After understanding the difference between ViewModel and LiveData, please read more about the MVVM pattern.

The View Model in Model-View-View-Model is not the same as Jetpack's ViewModel and LiveData. View Model in MVVM is a concept, you can create your own View Model using normal Java class. As long as it conforms to the MVVM behavior.

like image 163
aldok Avatar answered Nov 15 '22 07:11

aldok


As per Google Docs, if you are already using a library like Rx or Agera, you can continue using them instead of LiveData. But in this case, it is your responsibility to handle object allocation and de-allocation per Android components life cycle.

When working with MVVM pattern, since viewmodel has no reference to view, you'll need observable data holder to observe changes so you can properly update your view.

In this case you can use livedata, one of architecture components or any other observable like from Rx.

The main difference is livedata respects android lifecycle, and rx observables don't.

With livedata, there would be no crash due to stopped activity when onChange called since it's lifecycle aware. But without livedata,it is up to you to handle this case.

like image 41
Chan Myae Aung Avatar answered Nov 15 '22 08:11

Chan Myae Aung


The MVVM pattern is to only talk down V -> VM -> M and react up M -> VM -> V. Meaning the View can call methods on the ViewModel but the ViewModel doesn't have a reference to the View to call methods on it (setting data). The way to communicate from the ViewModel to the View is by the View observing some variable (ObservableField, LiveData, RxJava, etc).

LiveData is a great observable object to provide communication between the viewModel and the View especially over state changes like rotation. It also provides great communication between the Model and the View in regards to database changes and Room.

like image 24
Ge3ng Avatar answered Nov 15 '22 07:11

Ge3ng