In a mobile loan-application form, which state belongs in memory, which in small saved UI state and which on disk, and why?
answer
- three places, three lifetimes
- rebuild versus process death versus everything
- the bookmark, not the book
- identifiers and a step index
- the draft and the photo live on disk
basics
~20 sMemory holds the loaded screen state; it survives a UI rebuild but not process death. Small saved UI state holds the draft's identifier and step index needed to rebuild. Disk holds the draft and the ID photo, which must survive everything.
solid answer
~50 sEach place has a different lifetime, so each gets different data. **In memory**, a state holder that outlives the view keeps the loaded draft, validation errors and the employers I fetched: fast, and it survives a system-driven UI rebuild such as rotation or a language change, but it dies with the process. **Small saved UI state** holds only what I need to rebuild the screen after process death: the draft's identifier, the current step index, perhaps which field had focus. It is serialized, slower and size-limited, so no large objects and certainly no photo. **On disk** goes the draft itself, written as the user edits, and the ID photo as a file the draft points to. After a kill, saved UI state says `draft 42, step 4`, and the screen reloads draft 42 from disk.
go deeper
Remember the three places and the event that wipes each one: a rebuild, process death, or a deliberate dismissal. Then place the draft on disk.
Explain why saved UI state holds identifiers and a step index, not data, and walk the restore path from those identifiers back to the draft on disk.
Defend the split under pressure: debounce cost versus loss window, the photo as a file, and why two copies of the draft drift apart.
Turn the split into a team convention every screen follows, with a review checklist for what may go into saved UI state and what must be persisted.
## Three places, three lifetimes A mobile screen's state can live in three places, and the reason to know all three is that each one is lost by a **different event**. Both native platforms' own guidance draws the same split: in-memory state, a small **saved UI state** that the system keeps for you across process death, and **persistent storage** that you write yourself. | Place | Survives a system-driven UI rebuild | Survives OS-initiated process death | Survives a deliberate user dismissal | Suitable contents | Cost | |---|---|---|---|---|---| | **Memory** (a state holder outside the view) | Yes | No | No | Loaded data, derived UI state, anything rebuildable | Fastest | | **Small saved UI state** | Yes | Yes | Not reliably | Identifiers, a step index, a query, scroll or focus | Serialized, size-limited | | **Disk** (the app's own storage) | Yes | Yes | Yes | The user's data: the draft, files, records | Slower, needs a schema | Two definitions for the table: - A **system-driven UI rebuild** is the OS changing something about how the screen is drawn while the process stays alive: rotation, a window resize in split screen, a language or theme change. On one platform the default is to destroy and recreate the screen's UI objects; on the other, the views are usually re-laid out in place; cross-platform frameworks rebuild their widget tree. In every case, state held **only inside a view** is at risk, and state held in an object that outlives the view is not. - **Process death** is the OS ending the whole process to reclaim resources while the app is in the background, with no shutdown code guaranteed to run. ## Mapping the loan application - **Current step index and draft identifier** go into small saved UI state. They are tiny, and they are exactly what is needed to find the user's place again. - **The draft** (name, address, income, employer) is written to disk as the user edits, on a short debounce. It is the user's work and must survive even a reboot. - **The ID photo** is a file in the app's private storage, and the draft stores its path or identifier. A multi-megabyte image belongs nowhere else. - **Employer search results and validation messages** live in memory. They can be recomputed or refetched from the draft and the server. - **Which field had focus or a dialog that was open** can go into saved UI state if losing it would confuse the user; it is small. ## Why not put the whole draft in saved UI state 1. **It is meant for small, simple values.** One platform's guide limits it to primitive types and simple, small objects and says to store an identifier and keep complex objects in local storage; the other's says not to include data already in persistent storage, only an identifier to find it. 2. **It is serialized**, and on one platform that work can happen on the main thread during a rebuild, causing visible stutter for large objects. 3. **It can disappear.** One platform discards it when the user completely dismisses the app; the other states that the system may discard preserved UI data at its discretion. Saved UI state is not a substitute for saving the user's data. 4. **It duplicates the disk.** Two copies of the draft drift apart; one source of truth on disk does not. ## Restoring by reference 1. The OS starts a new process and recreates the screen, handing it the saved UI state. 2. The screen reads `draftId` and `step` from that state. 3. Its state holder loads the draft from disk using `draftId` and moves to `step`. 4. Anything derived (validation, fetched lists) is recomputed or refetched. The same path also handles a UI rebuild with less work: the in-memory holder is still alive, so it simply hands the existing state to the new views. ## Common mistakes - Believing a state holder that survives rotation also survives process death. - Pushing the whole draft, or the photo, into saved UI state "so it comes back". - Writing the draft to disk only at submission, so a kill on step four loses three steps. - Testing only rotation and concluding the form is safe. Choosing which on-disk store holds the draft, and encrypting personal data at rest, are separate design questions; this one is about which lifetime each piece of state needs.
- Where does the text the user is typing into a field right now live?In memory while they type, written to the on-disk draft on a short debounce or when the field loses focus. If losing the last few characters is unacceptable, saved UI state can also carry that one unsaved field value, as one platform's own guidance illustrates, because it is small. The bulk of the draft still lives on disk.
- Why is the ID photo stored as a file rather than inside the draft record or saved UI state?A photographed document is megabytes. In memory it dies with the process; in saved UI state it is far beyond what that mechanism is meant to carry. As a file in the app's private storage, referenced by the draft, it survives everything and loads only when needed. Which store holds the record and how the file is protected are separate decisions.
- If the draft is on disk, does the screen still need saved UI state?Yes, for what the disk does not know: which draft this screen shows, which step the user was on, the scroll position or an open dialog. Without it, the app knows a draft exists but not that the user was on step four. Some teams store the step inside the draft itself, leaving saved UI state with only the draft identifier.
A desk, a bookmark and a filing cabinet. Papers spread on the desk (memory) are fastest to work with but vanish when the cleaner clears the room. A bookmark slip (saved UI state) survives the clearing, but it only says which folder and which page, not what is written there. The filing cabinet (disk) holds the actual document and survives everything. You keep the bookmark small and the document in the cabinet.
- Keeping the form in a view model keeps it safe across process death.
- Saved UI state is a good place for the whole draft and the photo.
- A UI rebuild never loses state, so only process death needs design.
- Saved UI state survives the user swiping the app away.
- The draft only needs to reach disk when the user taps submit.