架构(Architecture)

A collection of 6 posts
Observation

Observation|创建 ViewModel 实例的方式

Combine 在 Combine 框架中,通常使用 @StateObject 在 View 中创建 ViewModel 实例。 ViewModel 实例的生命周期: * @State 跟随 View * @StateObject 独立管理,View 重建时保持 以上,是很好的实践方案。 Observation 中的问题 但在 Observation 框架中,不再能使用 @StateObject。如果使用 @State,会导致 ViewModel 预期之外的创建。 实际情况很复杂,并不是每次 View 更新都会导致 ViewModel 重建。例如如果有多个 View 使用同一个 ViewModel(例如 SummaryView 和 DetailView),就会导致同时存在多个 ViewModel
2 min read
MVVM 架构|协调 Service 层与Manager 层
架构(Architecture)

MVVM 架构|协调 Service 层与Manager 层

在使用 MVVM 架构的 Swift 项目,可以抽象出 MVVM 核心层和支持层。 * 核心层包含基本的 Model、View 和 ViewModel。 * 支持层包含 Service、Manager 和其他工具类(Utilities)。 一个完整的项目结构是这样: 在涉及 SwiftData 的功能模块,会更复杂一些。 Manager 和 Service 的协调关系 Service > Manager 典型的调用链路: View → ViewModel → Service → Manager → 外部资源 Service 层级高于 Manager,在 Service 调用多个 Manager。 在 ViewModel 中调用 Service * 避免在 View 中直接调用
5 min read