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
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.
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:
@Dao
public interface ItemDao {
@Query("SELECT * FROM ITEMS")
LiveData<List<Item>> getItemsWithChanges();
@Query("SELECT * FROM ITEMS WHERE ID = :id")
LiveData<List<Item>> getItemWithChanges(String id);
}
Now your repository can return LiveData from the DAO:
public class MyRepository {
public LiveData<List<Item>> getItems() {
// either handle "fetch if needed" here, or with NetworkBoundResource
return itemDao.getItemsWithChanges();
}
}
Which you get in your ViewModel:
public class MyViewModel extends ViewModel {
private final LiveData<List<Item>> items;
public MyViewModel(MyRepository repository) {
this.items = repository.getItems();
}
public LiveData<List<Item>> getItems() {
return items;
}
}
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).