Home > Software design >  In MVVM architecture, should the view model only return Live data to fragment?
In MVVM architecture, should the view model only return Live data to fragment?

Time:10-04

In MVVM architecture, should the view model only return Live data to fragment? Is it ok to return other primitive data type other than Live data also?

CodePudding user response:

You could return the data type that you need but in most cases you need to return Live data or StateFlow ( which is similar to Live data )

Because you want that observe in your fragment to get notified about any change.

There is no difference is term of architecture between LiveData<String> and String but it's just about what you need, if you need data that you can observe and change the UI of your fragment depending on that data use Live data and if you just need to access another primitive data type for some reason, it's totally fine.

CodePudding user response:

It depends on your case.

As we know ViewModel is the recommended UI-State holder, it maintains the state while changing the view configuration.

We usually use observable types LiveData, RxJava2, and Flow, so if any changes come from the data or domain layers or even from another view like "SharedViewModel" the UI will be continuously updated.

In most cases, we use observable types in the view model and non-observables in the view itself. We can see this UI event decision tree in the documentation. which helps us to configure where should we put the data.

UI event decision tree.

for more details. UI events State holders and UI State

  • Related