A field technician completes a job form in a basement with no signal and taps save. The app says saved. Whether head office ever sees that form is decided by a few lines of synchronization code that almost nobody on the product side has read.
Where the Screen Gets Its Data
Android's architecture guidance defines the goal: "An offline-first app is an app that is able to perform all, or a critical subset of its core functionality without access to the internet" (Android). The rule that delivers it is about one thing: "The local data source is the canonical source of truth for the app. It should be the exclusive source of any data that higher layers of the app read." The network is a delivery channel, not the place the app looks.
Three Ways to Write, One Decision
The same guide says "there are three strategies to consider" for writes, and choosing among them is a business decision. A payment must happen online or not at all. An analytics event can be lost. A customer's own work has to be written locally first and sent later, and for that the guide is direct: "This approach is the correct choice when the data is critical to the app."
The Bug That Passed Review
Our published example of that third strategy had a defect. When a send failed for any reason, it marked the record as failed, and then only ever retried records marked pending. One lost connection and the write was never sent again, while the app carried on looking healthy. The corrected version separates two cases. A network error leaves the record pending and retries. A rejection by the server is a real failure and is shown to the user.
Who Wins a Conflict
When two devices edit the same record offline, something must choose. In the approach the guide describes, devices attach a timestamp and the server "discards any data older than its current state while accepting those newer than its current state". The decision belongs on the server. Two phones that each decide locally can both conclude they won.
What to Ask Your Team
- After a failed sync, what state is the record in, and what retries it?
- Which of our writes must be online-only, and do the screens say so?
- Where is a conflict decided: on the device or on the server?
- Can a user see that something has not synced yet?
-Rocky
#DataIntegrity #OfflineFirst #MobileReliability #EngineeringDreams #StrategiaX
Originally published on ANDROID-ARCHITECT, the Android engineering blog of Strategia-X, with the sync code.
