What this thread does
Do first: use persistent storage with isStoredInMemoryOnly: false and call try modelContext.save() at an explicit editing checkpoint before acknowledging that edits are saved.
Short answer
As of November 2026, this card follows current SwiftUI/SwiftData docs. Autosave is useful but is not a user-visible save acknowledgment. Persist edits during the active session, retain a recoverable draft when saving fails, and treat background entry as an additional checkpoint rather than the sole save opportunity.
Choose / don't choose
- Autosave plus explicit checkpoints: choose when incremental edits may persist immediately; don't promise that Cancel reverses changes already committed (autosave).
- Explicit editor transaction: choose
autosaveEnabled = falseon the editor's owning context when Save/Cancel must delimit commits. Don't let an unrelated view save that context's pending edits. A detached draft value copied into the model only on Save provides clearer isolation; this is an application design choice. - Rollback: choose deliberate abandonment of all unsaved changes in that context; don't use it as generic error recovery while offering Retry (rollback.md)).
Not for: building list/detail navigation; see swiftui-list-detail-navigationsplitview.
Settings that matter
| Setting or flag | Value / default | Applies to | Source |
|---|---|---|---|
ModelConfiguration: isStoredInMemoryOnly | false for data that must survive relaunch; true is ephemeral | Current SwiftData storage API | property, storage guide |
ModelContext.autosaveEnabled | General default false; container main context is automatically set true. Set false deliberately for a manually committed editor | Current SwiftData contexts | property |
ModelContext.hasChanges | Check before saving; if false, avoid an unnecessary save | Current SwiftData save API | save.md) |
ModelContext.save() | try inside do/catch; only acknowledge success after it returns | Current SwiftData persistence API | save.md) |
ModelContext.rollback() | Discards pending inserts/deletes, restores changed models to their last committed state and clears undo history | Current SwiftData context | rollback.md) |
Order of operations
1. Create one intended persistent container near the root of the scene/view hierarchy; keep preview/test in-memory configuration out of production. SwiftData tracks inserted models and persists them through the associated context (storage guide). 2. Keep editor state such as dirty, saving, saved and save-failed distinct. At Save, copy draft changes into the intended context if needed, check hasChanges, and call try save(); on error show a concise failure and Retry while retaining the draft. Do not use try?, dismiss automatically or roll back the context in that catch (save API.md)). 3. Apple's ScenePhase reference says background may be followed by termination. .inactive may be temporary, so do not discard editor state then. Observe the relevant scene; an App-level value aggregates all scenes. 4. If a background save fails, preserve failure state to display on .active. Do not depend on presenting an alert while the scene is invisible. An in-memory draft alone cannot survive termination: save important drafts earlier to a durable store or journal whose errors are also surfaced. 5. Retry against the still-owned draft/context after correcting the underlying issue. Use rollback() only after explicit Discard and only when its context-wide scope is intended. Exercise relaunch after a successful save and simulate save failure to check that the UI never falsely reports success.
Known failures and fixes
| Error (short quote) | Cause | Fix | Source |
|---|
Keith Harrison's July 24, 2023 article demonstrates explicit hasChanges and save-error handling and notes an unspecified autosave timer. It supports explicit acknowledgments; it does not establish a guaranteed autosave delay.
Missing
- The linked source does not confirm: poirotsj Cancel/autosave report and its December 2025 date (forum response does not expose the report text).
- The linked source does not confirm: the linked WWDC session explains container/context ownership and saving (not shown by this row’s storage-guide URL).
- The linked source does not confirm: the suggested save checkpoints at both .inactive and .background.
- The exact
modelContainer(... inMemory: ...)parameter description was not available in readable primary-source text. Use the documented persistentModelConfigurationroute above; no modifier default is asserted. - Apple declares
save()as throwing without enumerating every store error on that page. Capture the real error category safely; no universal error string or retry policy is established. - Save-error retention of a particular model graph requires application checks; retain a separate draft when recovery must survive context replacement or termination.
Started from a post by @Apple, packaged by the editor. Original post