ios - 在 iOS 中采用 MVVM : ViewModel responsibilities?

标签 ios mvvm model viewmodel encapsulation

我想在我的下一个 iOS 项目中应用 MVVM 模式,我已经阅读了一些关于它的帖子和博客。但是他们展示的示例非常简单,没有业务逻辑,只有数据模型实体以及 ViewModel 更新。我不清楚实际上谁负责管理业务逻辑和操作:应该是 ViewModel 还是 Model

例如,假设我的一项功能需要:

  • 向 Web 服务请求一些数据
  • 解析此类数据并将其映射到我的数据模型实体
  • 对此类实体执行一些检查和操作,并使用结果更新ViewModel
  • 处理触发更多更新和操作的计时器

我应该如何分配这些职责以实现 MVVM

最佳答案

从MVVM中的Data Flow我们可以看出ViewModel的职责:

Data Flow in MVVM
1. UI calls method from ViewModel (Presenter).
2. ViewModel executes Use Case.
3. Use Case combines data from User and Repositories.
4. Each Repository returns data from a Remote Data (Network), Persistent DB Storage Source or In-memory Data (Remote or Cached).
5. Information flows back to the UI where we display the list of items.

关于 MVVM 的更多描述可以在这里:https://tech.olx.com/clean-architecture-and-mvvm-on-ios-c9d167d9f5b3

关于ios - 在 iOS 中采用 MVVM : ViewModel responsibilities?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40071341/

相关文章:

ios - 如何设置按钮的样式以具有透明文本?

c# - 为什么collection1 和collection2 有相同的值?

WPF DataTemplates - 为什么呈现不同?

ruby-on-rails - 带有 accepts_the_nested_attributes 的 belongs_to 不在对象中保存外键

ios - 在 UIActivityIndi​​catorView 旋转时禁用所有输入

iphone - Cell TextLabel 多行不再工作

ios - 为什么在使用 AFNetworking 库将 wave 文件上传到服务器时应该得到二进制响应

c# - Datagrid列可以在不同的行中包含不同类型的控件吗

Java - 使用文本区域将 JTable 设置为不可编辑

matlab - 解释 Wilkinson Notation 线性回归模型 matlab