重要前提
安装AI Skills的关键前提是:必须科学上网,且开启TUN模式,这一点至关重要,直接决定安装能否顺利完成,在此郑重提醒三遍:科学上网,科学上网,科学上网。查看完整安装教程 →
swift-data by pproenca/dot-skills
npx skills add https://github.com/pproenca/dot-skills --skill swift-data适用于 SwiftData 的综合性数据建模、持久化、同步架构和错误处理指南,与诊所模块化 MVVM-C 技术栈保持一致。
本技能强制执行与 swift-ui-architect 所要求的相同模块化架构:
┌───────────────────────────────────────────────────────────────┐
│ 功能模块:视图 + 视图模型,不导入 SwiftData │
├───────────────────────────────────────────────────────────────┤
│ 领域层:模型 + 仓库/协调器/错误协议 │
├───────────────────────────────────────────────────────────────┤
│ 数据层:@Model 实体,SwiftData 存储,仓库实现, │
│ 远程客户端,重试执行器,同步队列,冲突处理 │
└───────────────────────────────────────────────────────────────┘
核心原则: SwiftData 类型(@Model、ModelContext、@Query、FetchDescriptor)仅存在于数据层的实现代码中。功能视图/视图模型使用领域层类型和协议依赖。
广告位招租
在这里展示您的产品或服务
触达数万 AI 开发者,精准高效
本技能中的所有指南均假设采用诊所模块化 MVVM-C 架构:
Domain 和 DesignSystem(绝不导入 Data,也绝不导入同级功能模块)DependencyContainer、具体协调器和路由外壳的连线Domain 保持纯 Swift,定义模型以及仓库、*Coordinating、ErrorRouting 和 AppError 契约Data 拥有 SwiftData/网络/同步/重试/后台 I/O 并实现领域层协议在以下情况时参考本指南:
AsyncStream)在设计或重构基于 SwiftData 的功能时,请使用此工作流程:
Trip、Friend)(参见 model-domain-mapping、state-business-logic-placement)@Model 实体类(参见 model-*、model-domain-mapping)persist-repository-wrapper)ModelContainer,并包含错误恢复(参见 persist-container-setup、persist-container-error-recovery)state-dependency-injection)state-query-vs-viewmodel)crud-*)sync-*)rel-*)preview-*)schema-*)persist-model-macro、persist-container-setup、persist-autosave、schema-configurationquery-background-refresh、persist-model-actorquery-property-wrapper、state-wrapper-viewsschema-unique-attributes、sync-conflict-resolutionschema-migration-recovery、persist-container-error-recoverycrud-save-error-handlingsync-offline-first、sync-fetch-persistpersist-app-group、schema-configurationstate-query-vs-viewmodel、persist-repository-wrapper| 优先级 | 类别 | 影响 | 前缀 |
|---|---|---|---|
| 1 | 数据建模 | 关键 | model- |
| 2 | 持久化设置 | 关键 | persist- |
| 3 | 查询与过滤 | 高 | query- |
| 4 | CRUD 操作 | 高 | crud- |
| 5 | 同步与网络 | 高 | sync- |
| 6 | 关系 | 中高 | rel- |
| 7 | SwiftUI 状态流 | 中高 | state- |
| 8 | 模式与迁移 | 中高 | schema- |
| 9 | 示例数据与预览 | 中 | preview- |
model-domain-mapping - 在领域层/数据层边界将 @Model 实体映射到领域层结构体model-custom-types - 使用自定义类型而非并行数组model-class-for-persistence - 对 SwiftData 实体类型使用类model-identifiable - 使实体遵循 Identifiable 协议并使用 UUIDmodel-initializer - 为实体类提供自定义初始化器model-computed-properties - 使用计算属性处理派生数据model-defaults - 为实体属性提供合理的默认值model-transient - 使用 @Transient 标记非持久化属性model-external-storage - 对大型二进制数据使用外部存储persist-repository-wrapper - 将 SwiftData 封装在领域层仓库协议之后persist-model-macro - 对所有持久化类型应用 @Model 宏persist-container-setup - 在应用级别配置 ModelContainerpersist-container-error-recovery - 处理 ModelContainer 创建失败并进行存储恢复persist-context-environment - 通过 @Environment 访问 ModelContext(数据层)persist-autosave - 为手动创建的上下文启用自动保存persist-enumerate-batch - 使用 ModelContext.enumerate 进行大型遍历persist-in-memory-config - 对测试和预览使用内存配置persist-app-group - 使用 App Groups 进行共享数据存储persist-model-actor - 使用 @ModelActor 处理后台 SwiftData 工作persist-identifier-transfer - 跨 actor 传递 PersistentIdentifierquery-property-wrapper - 使用 @Query 进行声明式数据获取(数据层)query-background-refresh - 在后台上下文插入后强制视图刷新query-sort-descriptors - 对 @Query 应用排序描述符query-predicates - 使用 #Predicate 进行类型安全的过滤query-dynamic-init - 使用自定义视图初始化器进行动态查询query-fetch-descriptor - 在 SwiftUI 视图之外使用 FetchDescriptorquery-fetch-tuning - 调整 FetchDescriptor 的分页和待处理更改行为query-localized-search - 使用 localizedStandardContains 进行搜索query-expression - 使用 #Expression 处理可重用的谓词组件(iOS 18+)crud-insert-context - 通过仓库实现插入模型crud-delete-indexset - 通过仓库删除,使用来自 onDelete 的 IndexSetcrud-sheet-creation - 使用表单通过视图模型进行聚焦式数据创建crud-cancel-delete - 仅在保存时持久化,避免产生孤立记录crud-undo-cancel - 启用撤销并使用它来取消编辑crud-edit-button - 为列表管理提供 EditButtoncrud-dismiss-save - 在视图模型保存完成后关闭模态视图crud-save-error-handling - 处理仓库保存失败并提供用户反馈sync-fetch-persist - 使用注入的同步服务获取并持久化 API 数据sync-offline-first - 设计离线优先架构,包含仓库读取和后台同步sync-conflict-resolution - 为双向同步实现冲突解决rel-optional-single - 对可选关系使用可选类型rel-array-many - 对一对多关系使用数组rel-inverse-auto - 依赖 SwiftData 自动维护反向关系rel-delete-rules - 为拥有的关系配置级联删除规则rel-explicit-sort - 显式排序关系数组state-query-vs-viewmodel - 将所有数据访问路由通过 @Observable 视图模型state-business-logic-placement - 将业务逻辑置于领域层值类型和基于仓库的视图模型中state-dependency-injection - 通过 @Environment 注入仓库协议state-bindable - 使用 @Bindable 进行双向模型绑定state-local-state - 使用 @State 处理视图本地的临时数据state-wrapper-views - 提取包装器视图以处理动态查询状态schema-define-all-types - 定义包含所有模型类型的模式schema-unique-attributes - 使用 @Attribute(.unique) 处理自然键schema-unique-macro - 使用 #Unique 处理复合唯一性(iOS 18+)schema-index - 使用 #Index 处理热门谓词和排序(iOS 18+)schema-migration-plan - 在更改模型前规划迁移schema-migration-recovery - 为模式更改规划迁移恢复schema-configuration - 使用 ModelConfiguration 自定义存储preview-sample-singleton - 为预览创建 SampleData 单例preview-in-memory - 对预览隔离使用内存容器preview-static-data - 在模型类型上定义静态示例数据preview-main-actor - 使用 @MainActor 注解 SampleData阅读单独的参考文件以获取详细说明和代码示例:
| 文件 | 描述 |
|---|---|
| references/_sections.md | 类别定义和排序 |
| assets/templates/_template.md | 新规则模板 |
| metadata.json | 版本和参考信息 |
每周安装次数
61
代码仓库
GitHub 星标数
95
首次出现
2026年2月10日
安全审计
安装于
codex56
github-copilot55
gemini-cli55
kimi-cli54
opencode54
amp53
Comprehensive data modeling, persistence, sync architecture, and error handling guide for SwiftData aligned with the clinic modular MVVM-C stack.
This skill enforces the same modular architecture mandated by swift-ui-architect:
┌───────────────────────────────────────────────────────────────┐
│ Feature modules: View + ViewModel, no SwiftData imports │
├───────────────────────────────────────────────────────────────┤
│ Domain: models + repository/coordinator/error protocols │
├───────────────────────────────────────────────────────────────┤
│ Data: @Model entities, SwiftData stores, repository impls, │
│ remote clients, retry executor, sync queue, conflict handling │
└───────────────────────────────────────────────────────────────┘
Key principle: SwiftData types (@Model, ModelContext, @Query, FetchDescriptor) live in Data-only implementation code. Feature Views/ViewModels work with Domain types and protocol dependencies.
All guidance in this skill assumes the clinic modular MVVM-C architecture:
Domain + DesignSystem only (never Data, never sibling features)DependencyContainer, concrete coordinators, and Route Shell wiringDomain stays pure Swift and defines models plus repository, *Coordinating, ErrorRouting, and AppError contractsData owns SwiftData/network/sync/retry/background I/O and implements Domain protocolsReference these guidelines when:
AsyncStream)Use this workflow when designing or refactoring a SwiftData-backed feature:
Trip, Friend) with validation/computed rules (see model-domain-mapping, state-business-logic-placement)@Model entity classes with mapping methods (see model-*, model-domain-mapping)persist-repository-wrapper)ModelContainer once at the app boundary with error recovery (see persist-container-setup, )persist-model-macro, persist-container-setup, persist-autosave, schema-configurationquery-background-refresh, persist-model-actorquery-property-wrapper, state-wrapper-viewsschema-unique-attributes, sync-conflict-resolution| Priority | Category | Impact | Prefix |
|---|---|---|---|
| 1 | Data Modeling | CRITICAL | model- |
| 2 | Persistence Setup | CRITICAL | persist- |
| 3 | Querying & Filtering | HIGH | query- |
| 4 | CRUD Operations | HIGH | crud- |
| 5 | Sync & Networking |
model-domain-mapping - Map @Model entities to domain structs across Domain/Data boundariesmodel-custom-types - Use custom types over parallel arraysmodel-class-for-persistence - Use classes for SwiftData entity typesmodel-identifiable - Conform entities to Identifiable with UUIDmodel-initializer - Provide custom initializers for entity classespersist-repository-wrapper - Wrap SwiftData behind Domain repository protocolspersist-model-macro - Apply @Model macro to all persistent typespersist-container-setup - Configure ModelContainer at the App levelpersist-container-error-recovery - Handle ModelContainer creation failure with store recoverypersist-context-environment - Access ModelContext via @Environment (Data layer)query-property-wrapper - Use @Query for declarative data fetching (Data layer)query-background-refresh - Force view refresh after background context insertsquery-sort-descriptors - Apply sort descriptors to @Queryquery-predicates - Use #Predicate for type-safe filteringquery-dynamic-init - Use custom view initializers for dynamic queriescrud-insert-context - Insert models via repository implementationscrud-delete-indexset - Delete via repository with IndexSet from onDeletecrud-sheet-creation - Use sheets for focused data creation via ViewModelcrud-cancel-delete - Avoid orphaned records by persisting only on savecrud-undo-cancel - Enable undo and use it to cancel editssync-fetch-persist - Use injected sync services to fetch and persist API datasync-offline-first - Design offline-first architecture with repository reads and background syncsync-conflict-resolution - Implement conflict resolution for bidirectional syncrel-optional-single - Use optionals for optional relationshipsrel-array-many - Use arrays for one-to-many relationshipsrel-inverse-auto - Rely on SwiftData automatic inverse maintenancerel-delete-rules - Configure cascade delete rules for owned relationshipsrel-explicit-sort - Sort relationship arrays explicitlystate-query-vs-viewmodel - Route all data access through @Observable ViewModelsstate-business-logic-placement - Place business logic in domain value types and repository-backed ViewModelsstate-dependency-injection - Inject repository protocols via @Environmentstate-bindable - Use @Bindable for two-way model bindingstate-local-state - Use @State for view-local transient dataschema-define-all-types - Define schema with all model typesschema-unique-attributes - Use @Attribute(.unique) for natural keysschema-unique-macro - Use #Unique for compound uniqueness (iOS 18+)schema-index - Use #Index for hot predicates and sorts (iOS 18+)schema-migration-plan - Plan migrations before changing modelspreview-sample-singleton - Create a SampleData singleton for previewspreview-in-memory - Use in-memory containers for preview isolationpreview-static-data - Define static sample data on model typespreview-main-actor - Annotate SampleData with @MainActorRead individual reference files for detailed explanations and code examples:
| File | Description |
|---|---|
| references/_sections.md | Category definitions and ordering |
| assets/templates/_template.md | Template for new rules |
| metadata.json | Version and reference information |
Weekly Installs
61
Repository
GitHub Stars
95
First Seen
Feb 10, 2026
Security Audits
Gen Agent Trust HubPassSocketPassSnykPass
Installed on
codex56
github-copilot55
gemini-cli55
kimi-cli54
opencode54
amp53
Kotlin Ktor 服务器模式指南:构建健壮 HTTP API 的架构与最佳实践
1,400 周安装
persist-container-error-recoverystate-dependency-injection)state-query-vs-viewmodel)crud-*)sync-*)rel-*)preview-*)schema-*)schema-migration-recovery, persist-container-error-recoverycrud-save-error-handlingsync-offline-first, sync-fetch-persistpersist-app-group, schema-configurationstate-query-vs-viewmodel, persist-repository-wrapper| HIGH |
sync- |
| 6 | Relationships | MEDIUM-HIGH | rel- |
| 7 | SwiftUI State Flow | MEDIUM-HIGH | state- |
| 8 | Schema & Migration | MEDIUM-HIGH | schema- |
| 9 | Sample Data & Previews | MEDIUM | preview- |
model-computed-propertiesmodel-defaults - Provide sensible default values for entity propertiesmodel-transient - Mark non-persistent properties with @Transientmodel-external-storage - Use external storage for large binary datapersist-autosavepersist-enumerate-batch - Use ModelContext.enumerate for large traversalspersist-in-memory-config - Use in-memory configuration for tests and previewspersist-app-group - Use App Groups for shared data storagepersist-model-actor - Use @ModelActor for background SwiftData workpersist-identifier-transfer - Pass PersistentIdentifier across actorsquery-fetch-descriptorquery-fetch-tuning - Tune FetchDescriptor paging and pending-change behaviorquery-localized-search - Use localizedStandardContains for searchquery-expression - Use #Expression for reusable predicate components (iOS 18+)crud-edit-buttoncrud-dismiss-save - Dismiss modal after ViewModel save completescrud-save-error-handling - Handle repository save failures with user feedbackstate-wrapper-viewsschema-migration-recoveryschema-configuration - Customize storage with ModelConfiguration