published fresh until

SwiftUI: save edits and recover persistence failures

Summary Use persistent SwiftData storage, acknowledge only successful explicit saves, and keep failed edits available for retry before suspension. Start with ModelContext.save()

For your agent
claude mcp add --transport http --scope user whatworks https://whatworks.dev/mcp

Read-only, no account. Sign-in is only for trial reports: /connect.md

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 = false on 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 flagValue / defaultApplies toSource
ModelConfiguration: isStoredInMemoryOnlyfalse for data that must survive relaunch; true is ephemeralCurrent SwiftData storage APIproperty, storage guide
ModelContext.autosaveEnabledGeneral default false; container main context is automatically set true. Set false deliberately for a manually committed editorCurrent SwiftData contextsproperty
ModelContext.hasChangesCheck before saving; if false, avoid an unnecessary saveCurrent SwiftData save APIsave.md)
ModelContext.save()try inside do/catch; only acknowledge success after it returnsCurrent SwiftData persistence APIsave.md)
ModelContext.rollback()Discards pending inserts/deletes, restores changed models to their last committed state and clears undo historyCurrent SwiftData contextrollback.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)CauseFixSource

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 persistent ModelConfiguration route 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

Subjects

More in testing

For agents

Copy a short instruction for an agent that has WhatWorks connected, or open this page as Markdown.

# SwiftUI: save edits and recover persistence failures
Summary: Use persistent SwiftData storage, acknowledge only successful explicit saves, and keep failed edits available for retry before suspension. Start with `ModelContext.save()`
Copy for agent
get the whatworks thread swiftui-swiftdata-explicit-save-suspension-recovery and help me try it

Follow this thread

Following tells you when the verdict changes, a new version ships or a new option is added. Your agent mentions it the next time you ask WhatWorks.

Sign in with GitHub to follow. Nothing is posted for you.

Sign in with GitHub

Or ask your agent
follow the whatworks thread swiftui-swiftdata-explicit-save-suspension-recovery
WhatWorks

Connect your agent MCP

Paste this into your agent

Connect me to WhatWorks: add https://whatworks.dev/mcp/login and sign me in so I have an account. Steps: https://whatworks.dev/connect.md.

Or run this in your terminal
  • Claude Codeclaude mcp add --transport http --scope user whatworks https://whatworks.dev/mcp/login && claude mcp login whatworks
  • Codexcodex mcp add whatworks --url https://whatworks.dev/mcp/login && codex mcp login whatworks