Build log/Engineering

Offline first: building a mobile app for a basement with no signal

On a building site the signal drops exactly when someone needs to record something. Here is how an app keeps working, and keeps people’s trust, without a connection.

The short answer

  • Save to the phone first, every time, and sync when a connection returns.
  • Create record IDs on the device, so work can exist before the server knows about it.
  • Decide how conflicts resolve for each type of data, and never drop someone’s work silently.

The basement

An operative is in a plant room two floors underground. They photograph a weeping valve, note the time and add it to the site diary. There is no signal. If the app shows a spinner and then an error, they will write it on the back of a delivery note instead, and the software has lost.

Estimark is used in exactly these places, so working offline was never a later feature. These are the rules we build to.

Save to the phone first

Every change is written to a database on the device before anything else happens. The screen reads from that local database, so it is instant whether or not there is a connection. A separate process sends queued changes to the server when it can.

This reverses the usual order. Most apps ask the server and then show the result. An offline-first app shows the result and tells the server later.

IDs made on the device

If the server hands out IDs, nothing can be created offline, because nothing has an ID yet. We generate IDs on the phone, so a diary entry, its photos and the snag it raises can all refer to each other before any of them has been uploaded.

Photos are the heavy part

Text syncs in milliseconds; photos don't. They are compressed on the device, queued separately from the records that refer to them, uploaded in pieces that can resume, and shown with their own progress. A diary entry can appear in the office while its photos are still arriving.

Conflicts are a product decision

Two people edit the same record while both are offline. Something has to win, and the right answer depends on the data:

  • Diary entries and photos are append-only. Nothing is overwritten, so nothing conflicts.
  • Statuses such as "snag closed" take the latest change, with both changes kept in the history.
  • Money never merges automatically. A conflicting change to a valuation goes to a person.

Tell the person the truth

"Saved" must mean saved. The app says "Saved to phone, 3 changes waiting to sync" when that is the case, and shows when they have reached the office. People trust an app that is honest about its connection far more than one that pretends.

Test it like a site

We test with the network switched off, with a connection that drops every few seconds, and by force-closing the app halfway through a sync. If it survives those, it survives a basement.

Written by Flavoureak

We build and run Estimark, CurioKids and CalcStack, and we build web and mobile software for other businesses. Start a project.