L
L
labotsky962020-05-09 11:45:21
Android
labotsky96, 2020-05-09 11:45:21

How to track changes inside ViewModel?

Good afternoon. I am developing an application using the MVVM architecture. There is a fragment in which there is a RecyclerView with a list of users. The users themselves are stored in the Room database. A query to Room returns Livedata(List(User)). In the ViewModel, I get this list using the repository. What is the essence of the question. Before passing the list to RecyclerView, I need to modify it, add some data necessary for the adapter and pass not List (User), but List (ModifyUser) anymore. How to track inside the ViewModel that Livedata(List(User)) has changed and call the getModifyUser() method? Judging by the documentation on developers.android, I cannot subscribe to update objects inside the ViewModel. Now I got around the problem this way: in the fragment I subscribed to Livedata(List(User)), and to MutableLiveData(List(ModifyUser)), which are in the ViewModel. when LiveData(List(User)) changes, I call a method from the ViewModel in the fragment, which processes the User list and inserts it into MutableLiveData(List(ModifyUser)). And this is the list I'm already deriving. It seems to me that this method is similar to some kind of crutch. Surely there is a more elegant way to track LiveData changes inside the ViewModel

Answer the question

In order to leave comments, you need to log in

1 answer(s)
O
Oleg Zakharov, 2020-05-09
@labotsky96

You can use converting one LiveData to another using https://developer.android.com/reference/android/ar...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question