Reputation: 12262
I am learning about MVVM design pattern and trying to implement it in Android application at the moment. I am wondering how changes in a ViewModel are reflected in the View. different guides on internet show different things and some of them are suggesting that ViewModel(Observable) should notify changes to View(Observer) rather then Activity/Fragment update the ViewModel and trigger view to update.
What is the correct/best way to implement this? if I my ViewModel is not a Observable then is it that I am not following MVVM properly?
Upvotes: 0
Views: 561
Reputation: 9713
Generally, the View
must be aware of the ViewModel
, and the ViewModel
must be aware of the Model
, but expose the Model
to the View
. It looks something like this:
View ---knows about---> ViewModel ---knows about---> Model.
It is vital that these arrows stay in this direction, if the Model
knows about the ViewModel
then all the nice decoupling is lost.
It is the responsibility of the ViewModel
to shout out when a property has been changed, and anyone listening (your view) should act accordingly.
I'd recommend taking some time going through this tutorial.
Upvotes: 2