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.

  1. Two contexts change the same object and both save. What does each merge policy do, and what is the default? 13.10.1 · L5
    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…
  2. What is a fault, what does object uniquing guarantee, and why does a batch delete leave your objects out of date? 13.10.2 · L5
    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…
  3. What do delete rules do, and what happens when you set an attribute to a value of the wrong type? 13.10.3 · L3
    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.

  1. Which parts of a SwiftData context’s lifecycle need care: autosave timing, explicit saves, rollback, and what a predicate can express? 13.10.4 · L3
    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