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.
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.
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.
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.
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.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With