13.10 · Merge conflicts, faults and batch requests, delete rules, and the SwiftData context
Object-graph persistence moves the hard problems into your process: two contexts disagreeing, objects that are placeholders, and changes the framework makes without telling the objects you hold.
Mental model. A context is a scratch pad with its own copy of each object. Conflicts, faults and batch changes are all about which copy is current.
Core Data behaviour
Merge policies in a real conflict, faults and batch requests, and delete rules and type errors.
- Two contexts change the same object and both save. What does each merge policy do, and what is the default?
Core Data uses optimistic locking: each context remembers the row’s state when it loaded it and, at save time, the store checks whether the row changed in… - What is a fault, what does object uniquing guarantee, and why does a batch delete leave your objects out of date?
A fault is a placeholder: the object exists and has an identity but its attribute data has not been loaded, so memory stays low and fetches are cheap. In the… - What do delete rules do, and what happens when you set an attribute to a value of the wrong type?
Each relationship has a delete rule that says what happens to the objects on the other side when this object is deleted. Cascade deletes them too (deleting the…
SwiftData
The model context’s autosave, rollback, fetch descriptors and predicates.
- Which parts of a SwiftData context’s lifecycle need care: autosave timing, explicit saves, rollback, and what a predicate can express?
A ModelContext is SwiftData’s scratch pad, like a Core Data context. The main context (container.mainContext, also injected by .modelContainer) has…
More in Persistence & data
- 13.1 · Where data belongs, and the Keychain
- 13.2 · SQLite fundamentals: transactions, WAL, constraints, indexes and migrations
- 13.3 · The Core Data stack, contexts and faults, concurrency, 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.11 · Keychain status codes, detecting corruption, an LRU cache and library trade-offs