1---2name: ios-data-persistence3description: Use when designing iOS persistence, SwiftData migrations, Keychain storage, files, offline sync, or searchable app entities; use ios-architecture for broader module design.4---56# iOS Data Persistence7Acknowledgement: Shared by Peter Bamuhigire, techguypeter.com, +256 784 464178.89<!-- dual-compat-start -->10## Use When1112- Designing, implementing, or reviewing iOS persistence with SwiftData, Core Data migration, UserDefaults, Keychain, file storage, offline sync, Core Spotlight semantic indexing, App Entity exposure, AI local context, or privacy-sensitive caches.13- The task mentions local storage, migrations, offline-first sync, search/indexing, Siri/App Entity data exposure, or AI cache retention.1415## Do Not Use When1617- The task is general iOS implementation with no local data or cache concerns; use `ios-development`.18- The task is only platform capability routing; use `ios-platform-capabilities`.19- The task is security policy rather than persistence mechanics; use `ios-security-and-rbac` alongside this skill.2021## Required Inputs2223- Data model, sensitivity classification, deployment target, sync/backend contract, offline requirements, account/tenant model, deletion rules, and whether data is exposed to Spotlight, Siri, widgets, or AI context.2425## Workflow26271. Load `ios-development` for Swift and project standards.282. Choose storage: UserDefaults, Keychain, SwiftData/Core Data, FileManager, URLCache/NSCache, Core Spotlight, or an AI local-context cache.293. Load `references/ios-swiftdata.md` for SwiftData details.304. Load `references/semantic-indexing-and-ai-caches-wwdc26.md` when data is exposed to Siri, Spotlight, App Entities, widgets, or AI.315. Produce a model spec, migration plan, cache policy, deletion policy, and test plan.3233## Quality Standards3435- Persistence models must separate stored data, domain entities, DTOs, and search/intent projections when privacy or API shape differs.36- Secrets belong in Keychain or stronger platform storage, not UserDefaults, SwiftData, Spotlight, or logs.37- Offline data must have conflict, sync, stale-state, and deletion behavior.38- Indexed or AI-context data must have retention, invalidation, and user/account cleanup rules.3940## Anti-Patterns4142- Exposing stored models directly as App Entities without reviewing fields.43- Keeping stale Spotlight entries after logout, role change, tenant switch, or deletion.44- Using local AI context as a hidden long-term memory without retention and deletion controls.45- Treating SwiftData migrations as a compile-time concern only.4647## Outputs4849- Persistence model spec, storage decision, SwiftData migration plan, offline sync plan, search/index projection, AI cache policy, or review findings.5051## Evidence Produced5253| Category | Artifact | Format | Example |54|----------|----------|--------|---------|55| Data safety | Persistence model spec | Markdown doc per `skill-composition-standards/references/entity-model-template.md` covering entities, projections, retention, and deletion | `docs/ios/persistence-model-orders.md` |56| Correctness | Persistence test plan | Markdown doc listing CRUD, migration, sync, index, and deletion test cases | `docs/ios/persistence-tests-orders.md` |5758## References5960- `references/ios-swiftdata.md` for SwiftData `@Model`, relationships, model actors, migrations, CloudKit, and testing.61- `references/semantic-indexing-and-ai-caches-wwdc26.md` for Core Spotlight, App Entities, Siri semantic index privacy, local AI context, and cache invalidation rules.62- `references/skill-deep-dive.md` for UserDefaults, Keychain, repository pattern, file storage, URLCache/NSCache, offline-first architecture, and cross-skill references.63<!-- dual-compat-end -->6465## Storage Decision Guide6667## Inputs6869| Artefact | Produced by | Required? | Why |70|---|---|---|---|71| Entity and retention model | Product and backend owners | required | Defines durable records and deletion duties |72| API conflict contract | `api-design-first` | conditional | Governs offline reconciliation |73| Data classification | `ios-security-and-rbac` | required | Selects Keychain, file protection, or database storage |7475## Decision Rules7677| Data | Storage |78|---|---|79| Credential or private key | Keychain or Secure Enclave reference |80| Queryable application records | SwiftData with an explicit migration plan |81| Large media or export | Protected file with metadata in SwiftData |82| Financial offline conflict | Server-authoritative merge with idempotency |8384## Degraded Mode8586Without a runnable schema, provide the model delta, migration stages, backup/rollback path, and fixtures required for validation. Do not assert migration safety without testing old stores.87If execution is unavailable, report migration safety as unverified.8889## Domain Anti-Patterns9091- Storing tokens in UserDefaults. Fix: use an access-group-scoped Keychain item.92- Changing a persistent model without old-store fixtures. Fix: test each supported migration path.93- Caching private AI input indefinitely. Fix: define purpose-bound retention and deletion.94- Treating Spotlight as authoritative storage. Fix: rebuild the index from the source database.95- Resolving ledger conflicts by last-write-wins. Fix: reconcile against server versions.9697| Data Type | Storage | Example |98|---|---|---|99| User preferences | UserDefaults | Theme, language, sort order |100| Tokens / credentials | Keychain Services | JWT tokens, API keys, passwords |101| Structured app data | SwiftData or Core Data | Products, orders, customers |102| Searchable safe projections | Core Spotlight / App Entity projection | Title, display date, safe summary |103| Large files / images | FileManager | Photos, PDFs, exports |104| Temporary cache | URLCache / NSCache | API response caching |105| AI local context | App-owned encrypted cache or SwiftData projection | Prompt-safe summaries, never raw secrets |106107Rule of thumb: simple flag/scalar = UserDefaults. Secret = Keychain. Relationships/querying = SwiftData. Binary blob = FileManager. Search/Siri/AI exposure = separate projection with explicit privacy review.