AGENT SKILL
ios-persistence
Standards for SwiftData, Core Data, and Local Storage. Use when implementing SwiftData, Core Data models, or local persistence in iOS. (triggers: **/*.xcdatamodeld, **/*Model.swift, PersistentContainer, FetchRequest, ManagedObject, Query, ModelContainer, Repository)
- License 许可
- MIT
- 最近更新
- 2026-06-24
- Stars
- ★ 67
- Forks
- 24
01 前言 Front Matter
- name
- ios-persistence
- description
- Standards for SwiftData, Core Data, and Local Storage. Use when implementing SwiftData, Core Data models, or local persistence in iOS. (triggers: **/*.xcdatamodeld, **/*Model.swift, PersistentContainer, FetchRequest, ManagedObject, Query, ModelContainer, Repository)
02 资源文件 5
- 参考文档examples/3p-updates.md3.2 KB
- 参考文档examples/company-newsletter.md3.2 KB
- 参考文档examples/faq-answers.md2.3 KB
- 参考文档examples/general-comms.md602 B
- 许可证LICENSE.txt11.1 KB
03 SKILL.md 正文
iOS Persistence Standards
Priority: P0
Implementation Guidelines
SwiftData (Current iOS 17+)
- Models: Use the
@Modelmacro for your Swift classes. - Container: Use
@MainActorfor configuring theModelContainer. - UI State: Use
@Queryfor reactive data fetching in SwiftUI views. - Context API: Access
modelContextfrom the@Environmentfor CRUD (modelContext.insert, modelContext.delete, modelContext.save).
Core Data (Stable & Large Legacy)
- Mapping: Define your entities and attributes in the
.xcdatamodeldfile. - Stack: Use
NSPersistentContainerto encapsulate the SQLite store. - Background: Perform heavy writes on
newBackgroundContext()to avoid UI lag. - Main Context: Use
viewContextonly for UI thread operations.
Local Persistence (Small Data)
- Keychain: Use for Auth tokens, passwords, and PII. Never store sensitive keys in
UserDefaults. - UserDefaults: Use for lightweight settings/flags (e.g.,
isDarkModeEnabled). - File System: Save images or PDFs to the
Documentsdirectory usingData.write(to:).
Anti-Patterns
- No heavy I/O on ViewContext: Use private background contexts.
- No string-based predicates: Use KeyPaths or generated helpers.
- No missing merge strategy: Set mergePolicy explicitly (e.g., mergeByPropertyObjectTrump).