A tester reports that SwiftUI's @SceneStorage never restores the open article after relaunch; how do you diagnose it and test scene restoration properly on iOS?
answer
- how was the app ended
- force quit discards preserved state
- destroyed scene, destroyed data
- saving time is not guaranteed
- stale ID after restore
basics
~20 sCheck how the app was ended: force-quitting from the app switcher discards preserved state, and destroying a scene destroys its @SceneStorage. Test by backgrounding the app, then stopping it from Xcode and relaunching; the system never guarantees when values are saved.
solid answer
~40 sFirst ask how the tester ended the app. Apple's state-restoration sample notes that, while debugging, the system deletes preserved state when the user force quits the app, and also when the app crashes at launch, so swiping it away in the switcher is the classic false alarm. On iPad, removing a window's snapshot from the switcher destroys that scene, and Apple says its `@SceneStorage` data goes with it. The documentation also gives no guarantee about when the value is persisted, so a selection made a moment before the kill may be lost. The reliable test is to background the app, stop it from Xcode, and relaunch. Then check the code: a unique key used in the view that reads it, and an article lookup that falls back when the restored ID no longer exists.
go deeper
Remember that swiping an app away in the switcher is a force quit, and that @SceneStorage is meant to come back after the system ends the app, not after you do.
Explain the documented limits: no guarantee when values are saved, data destroyed with the scene, preserved state dropped on force quit, and how to test from Xcode instead.
Diagnose methodically: separate the system discarding state from code that reads the wrong key or rejects a stale ID, and add a fallback for restored IDs that no longer resolve.
Treat restoration as best effort in the product: decide which per-window state is worth restoring, and make every screen correct when nothing comes back.
## What @SceneStorage actually promises `@SceneStorage` is SwiftUI's property wrapper for **per-scene state restoration**: a value that "works very similar to `State`, except its initial value is restored by the system if it was previously saved". A **scene** is a SwiftUI `Scene` instance, typically one window. Apple's documentation limits the promise in ways that explain most "it doesn't restore" reports: - The system manages saving and restoring, and "makes no guarantees as to when and how often the data will be persisted." - If the scene is **explicitly destroyed** (the switcher snapshot is destroyed on iPadOS, or the window is closed on macOS), the data is destroyed too. - The backing data is not available to you, so you cannot inspect it directly; you observe it only through the wrapper after a relaunch. Restoration is for the case Apple's sample describes: the person leaves the app, the system **terminates it to free resources**, and the person returns expecting to be where they left off. ## Why the tester sees nothing Work through the causes from most to least common: 1. **The app was force quit.** Apple's state-restoration sample notes that, when debugging, the system deletes preserved state when the user force quits the app, as a safety precaution. Swiping the app away in the switcher is a force quit. 2. **The scene was destroyed.** On iPad, removing that window's snapshot from the switcher is the person saying "I'm done with this window"; its scene storage goes with it. 3. **The app crashed at launch** on an earlier attempt; the sample notes that the system deletes preserved state in that case too. 4. **The value was never saved.** The change happened just before the process was killed, and the system had not persisted it yet. 5. **The code reads a different key**, or the view that holds the `@SceneStorage` is not the one that drives the screen, so the restored value is never used. 6. **The value was restored but rejected**: the restored article ID no longer matches anything in the data layer, and the view shows the list without saying why. ## A test procedure that works 1. Run the app from Xcode, open an article, and give the system a moment. 2. Send the app to the background (Home gesture, or the Home button on older devices). 3. Stop the app from Xcode rather than from the switcher. 4. Relaunch from Xcode and confirm the article reopens. 5. On iPad, repeat with two windows showing different articles and confirm each window restores its own. 6. Repeat once after deleting the restored article from the data layer, to exercise the fallback. Apple's sample describes the same technique: do not use the app switcher to force quit during debugging; stop the app from Xcode instead, or suspend it with the Home button and then stop the debugger. ## What survives which ending | How the app ended | `@State` | `@AppStorage` | `@SceneStorage` | |---|---|---|---| | Backgrounded, then terminated by the system | Lost | Kept | Restored, if it was saved | | Stopped from Xcode after backgrounding | Lost | Kept | Restored, if it was saved | | Force quit from the app switcher | Lost | Kept | Discarded, per Apple's sample | | Window's scene destroyed (iPad switcher snapshot) | Lost | Kept | Destroyed with that scene | | App deleted | Lost | Lost | Lost | ## Making restoration robust in code - Use a **unique, scoped key** per value, such as `"reader.openArticleID"`, so two features never collide. - Store an **identifier**, not the model, and make the property **optional** so "nothing selected" is representable. - **Validate** the restored ID against the data layer and fall back cleanly when it is stale. - Treat restoration as best effort: the screen must make sense when nothing comes back. - Keep restorable state small and per scene; app-wide preferences live in `@AppStorage`, which is not affected by how the scene ended. - Add the Xcode stop-and-relaunch steps to the feature's manual test notes, so the next tester does not repeat the switcher mistake. Interviewers asking this want two things: knowing that **force quit is not a restoration test**, and knowing the documented limits well enough to separate "the system discarded it" from "our code ignored it".
- Why does the system discard preserved state when the user force quits the app?Apple's sample calls deleting the preserved state on force quit a safety precaution. The reasoning is that a force quit often signals a stuck app: if the restored state is what made the app misbehave, restoring it again would trap the user. That is also why the sample notes preserved state is deleted when the app crashes at launch.
- How should the reading screen behave when a restored @SceneStorage article ID no longer exists?Treat it as no selection: resolve the ID against the data layer, and when the lookup fails, show the list or an empty state and clear the stored ID so the next launch starts clean. Never crash or show stale content because a restored value is out of date.
- Swiping the app away in the switcher is a valid restoration test.
- @SceneStorage saves synchronously on every assignment.
- Removing an iPad window's snapshot keeps its @SceneStorage for later.
- A restored @SceneStorage ID can always be trusted to exist.