简体   繁体   中英

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 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 first Activity in the second one

Thanks a lot!

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM