[英]android - What is the 1:n relationship between view and viewModel
I know view and viewmodel are 1:n.我知道视图和视图模型是 1:n。 However, in most examples there was one viewmodel per activity.但是,在大多数示例中,每个活动都有一个视图模型。 ex) mainActivity - mainViewModel, userListActivity - userListViewModel例如)mainActivity - mainViewModel、userListActivity - userListViewModel
Shouldn't it be like below if it's a 1:n relationship??如果是 1:n 关系,不应该像下面这样吗? ex) mainActivity - viewModel, userListActivity - viewModel例如)mainActivity - viewModel、userListActivity - viewModel
What does 1:n mean? 1:n 是什么意思? Does it mean n activities in one viewmodel?这是否意味着一个视图模型中有 n 个活动? Does it mean n xml files in one viewmodel?这是否意味着一个视图模型中有 n 个 xml 文件?
Yes the common use-case is 1:1 .是的,常见的用例是1:1 。 But theoretically you could have multiple ViewModels serving a view, so that's why the 1:n relation.但理论上你可以有多个ViewModel服务于一个视图,所以这就是1:n关系的原因。
In practice this could be useful if you'd like to share a ViewModel between two views for example, or when you have a shared Flow ViewModel实际上,如果您想在两个视图之间共享ViewModel ,或者当您拥有共享的Flow ViewModel时,这可能会很有用
So basically in that flow every View could communicate with two ViewModels : the shared one and one specific for that view:因此,基本上在该流程中,每个View都可以与两个ViewModel通信:共享一个和一个特定于该视图:
SignUp
flow, with two screens user credentials (email, password, etc...) and another one with some preferences (like membership, notification, address or other app specific settings)想象一下一个SignUp
流程,有两个屏幕用户凭据(电子邮件、密码等),另一个屏幕有一些偏好(如会员资格、通知、地址或其他应用程序特定设置)SharedFlowViewModel
could hold all the user information, and at the last screen the Submit will send the request.在这种情况下, SharedFlowViewModel
可以保存所有用户信息,并且在最后一个屏幕上, Submit将发送请求。 All other screens with the individual ViewModel
s would just handle the individual screen logic: form validation, input enabling/desabling, etc...具有单个ViewModel
的所有其他屏幕将只处理单个屏幕逻辑:表单验证、输入启用/禁用等...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.