I'm working on an app using the MVVM
pattern with RxJava
. The architecture is the following:
It's the first time i use this pattern and i'm not sure about the best way to update a ViewModel
(and consequently the corresponding View
) when a change occurs in the data, made by another component of the application.
For example: suppose we have an Activity
showing the list of users i follow (like a social app), from this list i select a user and open his profile in another Activity
. Now, from this second Activity
i decide to unfollow the user and when i press the back button to return to the first Activity
i would like the list to be updated automatically (deleting the corresponding user, obviously without having to re-download all the data).
The problem is that the two Activity
have two different ViewModel
. How can i make the changes made by the second Activity
affect the ViewModel
of the first one? Is it the responsibility of the Repository
to inform the first Activity
of the changes?
- I'd rather not use
startActivityForResult
- I'd rather not to inject the
ViewModel
of the firstActivity
in the second one
Thanks a lot!
I thought you have a Repository that wraps a "Model" (local datasource) that is able to expose
LiveData<*>
, no?In which case all you need to do is this:
Now your repository can return LiveData from the DAO:
Which you get in your ViewModel:
And if you observe this, then when you modify the item in Room, then it'll automatically update this LiveData in
onStart
(when you start observing again).