视图模型(View Model)在使用单Activity架构时可能会变得较为庞大。

huangapple go评论83阅读模式
英文:

View Model can be heavy if we use single activity based architecture

问题

根据谷歌建议的单Activity架构应用程序。我遇到一个需要澄清的情况。

我有一个包含3个片段的活动,每个片段都链接到不同的片段,其中包含其他屏幕。

  1. 对于每个标签,我是应该使用单独的ViewModel,还是应该使用一个ViewModel来管理不同的屏幕。

  2. 由于我只有一个活动,并且ViewModel会一直存在直到活动被销毁。我为每个屏幕创建的所有ViewModel是否会包含数据,直到活动被销毁。如果是这种情况,是否会使我的应用变得很重。

英文:

As google suggests single activity based application. I have a situation where I need clarification.

I have one activity containing 3 fragments and each fragment is linked to different fragments having other screens.

  1. should I use only one viewmodel for each tabs or should I use one view model having different screens.

  2. As I have only one activity and view Model resides till activity is destroyed. Do all viewModel that I will create for each screen will contain data until activity is destroyed. If this is the case will it make my app heavy.

答案1

得分: 6

  1. 应该为每个标签使用尽可能小的ViewModel范围。通常情况下,这意味着仅与一个片段相关联的数据应该使用仅与该片段相关的ViewModel。

  2. 因为我只有一个活动,并且ViewModel保持到活动被销毁。我为每个屏幕创建的所有ViewModel是否都会包含数据,直到活动被销毁。如果是这种情况,是否会使我的应用变得沉重。

ViewModel只在它们附加的ViewModelStore存在的时候存在。因此,如果您的ViewModel与一个片段相关联,它将仅在该片段存在的时间内存活。例如,如果该片段从后退栈中弹出,或者您调用了remove()方法,那么ViewModel将被销毁。只有当您明确使用活动作为ViewModelStoreOwner创建它们时(例如,通过使用ViewModelProvider(activity)),ViewModel才会与您的活动一样长久存在。

英文:

> 1. should I use only one viewmodel for each tabs or should I use one view model having different screens.

You should use the smallest scope for each ViewModel possible. Generally, this means that data associated with only one fragment should use a ViewModel associated with just that one fragment.

> 2. As I have only one activity and view Model resides till activity is destroyed. Do all viewModel that I will create for each screen will contain data until activity is destroyed. If this is the case will it make my app heavy.

ViewModels live only as long as the ViewModelStore they're attached to is around. Therefore if you have a ViewModel associated with a fragment, it'll survive only as long as that fragment exists. For example, if that fragment gets popped off the back or you call remove(), then the ViewModel is destroyed. ViewModels only live as long as your activity if you specifically create them using the activity as the ViewModelStoreOwner (for example, by using ViewModelProvider(activity)).

答案2

得分: 2

> 我是应该为每个选项卡使用一个ViewModel,还是应该使用一个包含不同屏幕的ViewModel?

在单Activity架构中,您可以为ViewModel选择使用Activity范围或Fragment范围。如@ianhanniballake在他的回答中建议的那样,使用与Fragment关联的ViewModel,这意味着在您的情况下,每个选项卡都将有自己的ViewModel附加到Fragment上。这将为每个选项卡中的功能提供关注点分离。

> 由于我只有一个Activity,并且ViewModel在Activity销毁之前都会存在。我为每个屏幕创建的所有ViewModel是否都会包含数据,直到Activity销毁。如果是这种情况,是否会使我的应用变得很重。

再次回答这个问题与您的第一个问题以及提供的解释有关。

在使用单Activity架构时,尽量缩小范围。仅将ViewModel与Activity相关联(我们称之为全局ViewModel),以保存跨多个Fragment使用的数据。这样,ViewModel将在Activity位于返回堆栈时包含数据,并且Fragment可以在需要时使用它。
这将为您带来两个优势:

  • ViewModel将不包含您在第二个问题中提到的额外数据
  • 最重要的是,在多次访问Fragment时不会出现任何数据不一致的情况。例如,如果您打开任何Fragment,从API获取数据并将其保留在全局ViewModel中,但在从返回堆栈中删除Fragment时未能删除它,那么下次打开Fragment时该Fragment将显示旧的/过时的数据。为了避免这种情况,最好将此数据放在与Fragment范围相关的ViewModel中。

希望这对您有所帮助。

英文:

> should I use only one viewmodel for each tabs or should I use one view
> model having different screens?

In single activity architecture, you can use ViewModel with either Activity Scope or with Fragment scope. As @ianhanniballake suggested in his answer, use ViewModel with Fragment which means that each tab in your case will have its own ViewModel attached to fragment. This will provide separation of concerns for functionality in each tab.

> As I have only one activity and view Model resides till activity is
> destroyed. Do all viewModel that I will create for each screen will
> contain data until activity is destroyed. If this is the case will it
> make my app heavy.

Again answer to this question is connected to your first question and explanation provided to it.

While working with single activity architecture narrow down scope as much as possible. Use ViewModel associated with activity (lets call it Global ViewModel) only to keep data which is used across multiple fragments. This way viewmodel will contain data till activity is in backstack and fragments can use it when required.
This will provide you 2 advantages

  • ViewModel will not contain additional data which you mentioned in your 2nd question
  • And most importantly there will not be any data discrepancies while accessing fragment multiple times. For e.g. if you open any fragment, fetch data from api and keep it in Global ViewModel and fail to delete it when remove fragment from backstack then your fragment will show old/obsolete data when you will open fragment next time. To avoid this scenario it is better to keep this data in viewmodel with fragment scope.

I hope this will be useful.

答案3

得分: 1

  1. 每个片段通常应该有自己的 ViewModel,但在某些情况下,如果你想共享同一个 ViewModel 实例,你可以通过创建一个作用域为活动的 ViewModel来实现。

  2. ViewModel 对象的作用域限定在传递给 ViewModelProvider 的生命周期范围内,可以在文档中找到相关信息,你可能需要阅读其他详细内容,但从技术上讲,你可以将 ViewModel 的作用域限定为一个片段或一个活动。

英文:
  1. Each Fragment usually should have a ViewModel itself but in some cases where you want to share the same ViewModel instance, you can achieve it with having a ViewModel scoped to an activity.

  2. ViewModel objects are scoped to the Lifecycle passed to the ViewModelProvider when getting the ViewModel it is found on the docs and you might have to read other details there but technically you can scope a ViewModel to a Fragment or to an Activity.

huangapple
  • 本文由 发表于 2020年4月5日 13:43:14
  • 转载请务必保留本文链接:https://go.coder-hub.com/61038532.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定