iOS Persistence
Priority: P0
Implementation Workflow
- Choose storage tier — SwiftData for iOS 17+, Core Data for legacy, Keychain for secrets, UserDefaults for flags only.
- Define models — Use
@Modelmacro (SwiftData) or.xcdatamodeld(Core Data). - Configure container — Use
@MainActorforModelContainer(SwiftData) orNSPersistentContainer(Core Data). - Perform background writes — Use
newBackgroundContext()(Core Data) to avoid UI lag; never heavy I/O onviewContext. - Secure sensitive data — Use Keychain for tokens and PII; never store in
UserDefaults.
See SwiftData and Core Data implementation examples
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
mergePolicyexplicitly (e.g.,mergeByPropertyObjectTrump)