Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MVVM: Complex View/ViewModel -> Multiple LiveData objects?

Most of the MVVM examples are dealing with very simple user interfaces.

But lets say I have an activity with many views to update (i.e. lots of data)

As I read in other places, multiple ViewModel objects is a bad pattern.

So, as I see it there are two solutions for that:

  1. Create a single object (and single LiveData for it), that wraps all other data objects.
    But there's a problem with this - each data object that gets updated will cause the whole UI to update.

  2. Create multiple objects (and multiple LiveData objects for it).
    It means that I need to observe each LiveData object. Is there a problem with this pattern?

Thanks in Advance!

like image 641
dor506 Avatar asked Oct 16 '22 11:10

dor506


1 Answers

First Point you mentioned : Yes this is not optimal Pattern to do but if you have small data then, separating LiveDatas is more work for less gains

Second Point you mentioned : Yes this is more optimal, you can have a LiveData object for each View you want to update and observe them all from your activity or fragment. There are no issues in this Pattern.

About Mutilple ViewModels : Multiple ViewModels Pattern in same Activty/Fragment is also an option if you have too many things(LiveData objects or funcitions) happening in one ViewModel. This is only recommended to make viewModels lighter. So only use this if you are having a large viewModel class

like image 131
Pavan Varma Avatar answered Oct 20 '22 21:10

Pavan Varma