简体   繁体   中英

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? Is it ok to return other primitive data type other than Live data also?

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.

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事件决策树 .

for more details. UI events State holders and UI State

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