Activity with multiple ViewModels
Asked Answered
B

4

39

I have an Activity that contains 3 RecyclerViews. I need populate RecyclerViews with data from remote repository (3 different requests). Can I use multiple ViewModels in the Activity, or is there any better solution (best practice).

Beekeeping answered 21/9, 2017 at 9:22 Comment(1)
Google sample of AAC showed the usage of 1 RecyclerViews with 1 LiveData. What is the issue if you try to do 3 RecyclerViews with 3 LiveDatasRuppert
B
23

In this case I would recommend to use one view model which populates three different LiveData objects. This way the UI can get updated whenever one of your three requests gets a response. For details how to use a RecyclerView with LiveData take a look into the Google Example.

I think having multiple viewmodels per activity only increases complexity and I do not see any value in doing that.

Berkowitz answered 2/11, 2017 at 17:46 Comment(2)
The Google example link is dead.Hayse
@Berkowitz There is a value in doing that: decreasing coupling between RecyclerViews and increasing the ability to reuse them independently or move to another screen.Bridgetbridgetown
B
57

According to the open/closed principle, you should create three different ViewModels. The complexity isn't increased that much, and you are gaining the ability to move one ViewModel (or just reuse it) with the corresponding RecyclerView to the another Activity very easily.

Of course, sometimes breaking rules makes sense - for example if you know, there is no chance, that RecyclerView will be reused or moved to another screen, and then you can go for simpler solution with one ViewModel.

The same situation if the ViewModel (even with the 3 lists) is likely to stay always very simple (just three LiveData fields, just a few lines of code to populate them), you can break this rule.

However violation of O/CP is not a good practice - it's just a conscious breaking of rule.

Bridgetbridgetown answered 4/3, 2018 at 12:39 Comment(2)
Is it possible for ViewModels to communicate between each other? Let's say one ViewModel has LiveData object, that triggers LiveData in another ViewModel. Tying them together in Activity seems wrong, so how would you do this?Practical
I don't know what is your use case but in general I'd avoid connecting one ViewModel to the another. I'd rather create another object and inject the same instance of it to the both ViewModels. Of course this third, common object can also have LiveData as a member, which one of the ViewModel can update and the other - observe. If you need more detailed info, please post a new question and I'd be glad to answer it.Bridgetbridgetown
B
23

In this case I would recommend to use one view model which populates three different LiveData objects. This way the UI can get updated whenever one of your three requests gets a response. For details how to use a RecyclerView with LiveData take a look into the Google Example.

I think having multiple viewmodels per activity only increases complexity and I do not see any value in doing that.

Berkowitz answered 2/11, 2017 at 17:46 Comment(2)
The Google example link is dead.Hayse
@Berkowitz There is a value in doing that: decreasing coupling between RecyclerViews and increasing the ability to reuse them independently or move to another screen.Bridgetbridgetown
B
4

I got two recyclerview in a fragment. I think that use two ViewModels would be better. Cause different recyclerviews got their own data request, and state handling especially connections error. In this case separate into different ViewModels would not increase the the complexity, but I think it well fit the rule of decupling

Bannister answered 1/9, 2018 at 9:11 Comment(0)
M
0

Even simpler, you can have one ViewModel, that uses one service class, which in turn uses the three repositories to get the data. For example:

XActivity --> XViewModel --> XService --> {Arepository, Brepository, Crepository}

Mannose answered 10/5, 2018 at 23:28 Comment(1)
This solution will make the coupling between Activity, ViewModel and Service very high so that moving one RecyclerView from XActivity to YActivity will be hard.Bridgetbridgetown

© 2022 - 2024 — McMap. All rights reserved.