13.3 · The Core Data stack, contexts and faults, concurrency, and migrations
Core Data is an object graph manager that happens to persist to SQLite. The stack, the context and the fault are the three ideas that explain nearly every behaviour and every bug.
Mental model. A context is a scratch pad of objects tied to one queue. Nothing reaches the store until you save, and nothing crosses between contexts by itself.
The stack and the object graph
Model, store, contexts, faults and validation.
- What are the parts of the Core Data stack, what is a managed object context, and what is a fault?
The stack has four parts. The managed object model (NSManagedObjectModel, the .xcdatamodeld) describes entities, attributes and relationships. The persistent…
Concurrency and evolution
Contexts and threads, merge policies, and lightweight migration.
- How do you use Core Data across threads: contexts, perform, merging changes and merge policies?
A managed object context is not thread-safe; it is bound to a queue. The rules: viewContext lives on the main queue and is for UI reads and small edits; do… - How do Core Data migrations work: lightweight, staged and heavyweight, and what about CloudKit?
When you ship a new version of the model, an existing store must be migrated to match, or Core Data refuses to open it. Edit a model by adding a new model…
More in Persistence & data
- 13.1 · Where data belongs, and the Keychain
- 13.2 · SQLite fundamentals: transactions, WAL, constraints, indexes and migrations
- 13.4 · SwiftData: models, contexts, predicates, actors and schema evolution
- 13.5 · The repository pattern and cache design
- 13.6 · Crashes mid-write, integrity, backups and data protection
- 13.7 · Syncing data across devices: conflicts, tombstones and CloudKit
- 13.8 · UserDefaults surprises and FileManager essentials
- 13.9 · Reading query plans, typing and NULL, WAL locking, savepoints and full-text search
- 13.10 · Merge conflicts, faults and batch requests, delete rules, and the SwiftData context
- 13.11 · Keychain status codes, detecting corruption, an LRU cache and library trade-offs