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.

  1. What are the parts of the Core Data stack, what is a managed object context, and what is a fault? 13.3.1 · L3
    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.

  1. How do you use Core Data across threads: contexts, perform, merging changes and merge policies? 13.3.2 · L5
    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…
  2. How do Core Data migrations work: lightweight, staged and heavyweight, and what about CloudKit? 13.3.3 · L5
    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