The unit of work is one data path from a user action to storage and back.
The durable artifact is a set of scripted tests for the failure modes: offline, late, duplicate, conflicting, and cancelled. The chief hazard is a path that is correct only when everything succeeds in order.
Trace an edit: the user toggles a job, the view model builds a PendingChange, the outbox writes it to the store, and the screen shows the job with the change applied and a pending indicator. Later, a flush sends the change with its identifier as the key, and on success removes it. Ownership is clear: the store owns durability, the outbox owns order and retry, the repository owns staleness, and the model owns what the screen shows.
Record the baseline with swift test --filter OutboxTests. Add a new operation, setNotes, to the change type if one does not exist, and write the test that it is applied once and in order before changing the outbox. Watch each test fail for the right reason first.
Review failure paths before success paths. For every await, ask what happens if the process dies after it, if it is cancelled during it, and if it throws. Look for any write that happens after a network call, and for any state assignment that does not check that it is still current.
When data is wrong on a device, reproduce it with a scripted transport and an in-memory store. If a test cannot reproduce it, the abstraction is hiding something; add the missing seam rather than a sleep.
Tokens live in memory and in headers, never in logs, stores, or URLs. The base URL for a debug build must not be production. Stored data may contain customer information, so check file protection, and confirm that error messages and logs carry identifiers, not content.
Test with doubles that you control. A scripted transport returns responses in a chosen order. A gated search releases answers out of order. An in-memory store simulates a crash by creating a second instance over the same data. Avoid sleeps; if a test needs one, the code needs a seam.
Agree on one reference implementation for each data concern and keep it in the examples index. Require a failing test for any data defect before the fix. Review migrations as sensitive files, with a named owner.
- Failure cases are an enum, not strings.
- Only reads are retried, with a cap.
- Edits are persisted before any network call.
- Stored data has a version and a tested migration.
- State writes are guarded against stale requests.
- Why does the outbox stop at the first failure?
- What does the idempotency key protect against, and who must honor it?
- Why must the view model keep content on screen during a refresh?
- What would make the search test pass for code that ignores cancellation?
Continue with the book
Explore the full treatment, worked examples, and exercises in Agentic iOS App Development.
Companion article for Agentic iOS App Development, published by Choral Software. Article text and original examples © 2026 Choral Software. All rights reserved.
