In Part 1, I wrote about building Thryvo Cal so every part can be tested on its own. That foundation assumed one thing: the internet is always there.
For a food-tracking app, that assumption is wrong. People log meals in kitchens, restaurants, and cars, where the signal comes and goes. So the next question was simple: what should the app do when the connection disappears?
I set three requirements:
- Users can view and log food without a connection.
- When the phone and the server both hold data, they reconcile without losing anything.
- A dropped connection never crashes the app.
Decision 1: Keep a copy on the phone
The problem: If every screen waits on the server, a weak signal means a broken app, and the user did nothing wrong.
The decision: Food entries are now stored in a database on the device. The app reads and writes there first, so it feels the same online or offline.
This is where Part 1 paid off. The local database sits behind the same agreement the server already follows, so the rest of the app doesn’t need to know which one it’s talking to.
Decision 2: Make database updates safe
The problem: An app’s stored data changes over time. If the structure changes and older phones can’t adapt, users can hit errors or lose data after an update.
The decision: The local database is versioned, with defined steps for creating it fresh and for upgrading an older one. People who already have data on their phones can update safely.
Decision 3: Check the connection first
The problem: Waiting on a server that can’t be reached usually ends in an error, and an error is a poor way to discover you’re offline.
The decision: Before contacting the server, the app checks whether there’s an internet connection. If there isn’t, it skips the request and carries on with local data. Offline becomes a normal situation the app handles, not an emergency.
Decision 4: Reconcile the two copies
The problem: Once data lives in two places, the copies will drift apart. You log something offline, or use another device, and now the phone and the server disagree.
The decision: When the app loads the food list, it compares the two sides:
If they match, nothing happens. If the server has more, the missing items are copied to the phone. If the phone has more, the missing items are sent to the server.
Either way, the screen shows the phone’s copy. I also pulled this comparison into its own small piece instead of burying it in the loading logic, which made it much easier to read and test.
Decision 5: Keep syncing under test control
The problem: If syncing always runs, every test touches the network and results become unpredictable.
The decision: Syncing sits behind a simple switch, so it can be off for offline mode or ordinary tests and on when I want to test reconciliation specifically.
I write these tests in a Given-When-Then style, such as “given the server has two more items, when the list loads, then those items appear on the phone.” Each test reads like a plain sentence about behavior.
Decision 6: Record when something changed
The problem: Keeping two copies accurate over time takes more than knowing whether an item exists. The app also needs to know when it was last touched.
The decision: Each food item now carries a “last updated” timestamp, laying the groundwork for more accurate syncing.
The honest trade-off
Two-way syncing adds moving parts: a database to migrate, a comparison to get right, and more situations to test. I accepted that because the alternative was an app that stops working whenever the signal does.
The current comparison is also simple. It checks how many items each side has, which catches missing entries well, but it isn’t a complete answer to edited items or conflicting changes. The timestamp is there to help with that, and it’s the next thing I want to build on.
What I’d tell anyone building for real-world use
- Treat “offline” as a normal state, not an error.
- Keep tricky logic small and separate. The sync comparison is easy to test because it stands alone.
- Say what your solution doesn’t cover yet. It builds trust and shows you what to work on next.
In Part 3, I look back at how these two phases fit together. How do you handle offline users in your own apps?
#SoftwareArchitecture #OfflineFirst #Testing #Flutter #AppDevelopment #EngineeringDecisions