Both mobile platforms may end a backgrounded app's process to reclaim resources without running teardown code, and users swipe apps away and batteries die, so a queue held only in memory is lost. The outbox therefore lives as a table in the same embedded database as the inspection data. If the tick and its queue entry were committed separately, a kill between the two commits leaves either a tick the server will never hear about, or a queued tick the screen no longer shows. One transaction makes them commit or roll back together. Only after that commit does the app treat the change as saved on the device; sending is a later, separate step that reads the queue.
The situation
A technician inspecting pumps in a plant basement ticks "pressure relief valve tested" on a checklist. There is no signal. The app has to do two things with that tap: change the checklist item in its local database, so every screen shows it ticked, and remember that the server has not heard about it yet. The second thing is the outbox - a durable queue of pending writes that a sync step drains when the network returns. This question is about where that queue lives and how the two writes are tied together.
Why a queue in memory is not a queue
A mobile app does not decide when its process ends. Several things end it with no warning and no shutdown code:
- The OS reclaims memory. Both native platforms may end a backgrounded or suspended app's process to free resources, and neither promises that teardown code runs first.
- The user swipes the app away from the app switcher.
- The battery dies or the app crashes while work is still in memory.
In a field app all of these are routine: the technician pockets the phone, opens the camera, takes a call, and the app is gone before the van reaches coverage. A queue held in a list in memory is lost every time one of these happens; one written to disk only when the app goes to the background is lost whenever a crash or a flat battery comes first. So the queue must be on persistent storage before the app treats a change as saved. It must also be in the app's persistent storage, not in the OS's purgeable cache folder: the storage guides of both platforms say the system may delete cache files when the device runs low on space.
Why two durable writes are still not enough
Persisting the queue in a separate file, or committing the record and the entry in two separate transactions, opens a crash window between them. Which failure you get depends on the order:
| Write order | A kill lands between the writes | What the app finds after relaunch |
|---|
| Record first, then queue entry | after the record commit | The item shows ticked, but nothing will ever tell the server |
| Queue entry first, then record | after the entry commit | The server will receive a tick the local screen does not show |
| Both in one transaction | anywhere | Either both exist or neither does; at worst the technician taps again |
The window is short, but an app used all day by many technicians, on older phones under memory pressure, will hit it. Both failures are silent: no error appears, and the office simply receives a report that differs from what the technician saw.
The design: one store, one transaction
The fix is to keep the outbox as a table in the same embedded database as the inspection data, and to write both rows in one transaction:
- Open a transaction.
- Update the checklist item's row.
- Insert the outbox entry describing the change - operation, the record's id, payload, an idempotency key, an attempt count of zero.
- Commit. Only now does the UI treat the change as saved on this device.
- Nudge the sync step. The nudge is only a hint: if the app dies before it fires, the entry is still in the table and the next drain finds it.
Sending is therefore never part of the tap. The tap ends at the commit; delivery is a separate step that reads the queue, which is what lets the app keep working for hours offline. The same rule covers every write the technician makes - a new finding, a photo caption, closing the job.
"Saved" and "synced" are different words
Once the commit lands, the change is saved on this phone: it survives a kill, a reboot and a flat battery. It is not yet synced: the office cannot see it, and no copy exists anywhere else. Good designs keep those two states apart in what they show. How one record displays its pending state belongs to the design of optimistic state; the queue's job is to guarantee that what the screen shows and what will be sent can never drift apart.
What the transaction does not protect
The local commit protects against process death, not against losing the app's storage:
- an uninstall that removes the app's private files takes unsent entries with it;
- clearing the app's data, or a lost or reset phone, does the same;
- a device backup, where one exists, is not a route to the app's server.
So the outbox should drain as soon as coverage returns, and the app should show how much is still waiting - a technician who can see "12 changes waiting" does not reinstall the app to fix a glitch with a day's work unsent.