Skip to content Skip to sidebar Skip to footer

Android MVVM - Update ViewModel When Data Changes

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

Solution 1:

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).


Post a Comment for "Android MVVM - Update ViewModel When Data Changes"