Research note · 19 November 2026

Two devices, one calendar: three sync traps

Ahmet Turan Taşdelen · 19 November 2026 · about 5 minutes · Project: My Calendar App

In short: I wanted my calendar to sync between my iPhone and my Linux computer. “Write the whole calendar to the cloud and let the newest copy win” looks good enough at first, but it silently deletes changes made offline. I had to make every event its own record, keep deletions as markers, and stop a new device from writing anything before the cloud had really answered. I tested all of it on my computer with two separate “devices”.

Context

My Calendar App is a single web app: it runs as a PWA installed on the iPhone home screen and as a desktop app on Linux with Electron. Data lives first in the device's own storage. To let the phone and the computer see the same calendar, I added Google's Firebase (the Firestore database and email/password sign-in). Firestore can queue writes while offline and send them once the connection is back; the real question was how two devices' changes should be merged.

iPhoneWeb app installed on the home screen (PWA)Local copy and an offline write queue
Linux computerDesktop app (Electron)Local copy and an offline write queue
Google FirebaseSign-in: email and passwordusers/{uid}/events/{id}
users/{uid}/categories/{id}
Security rule: each user can read and write only their own data.
Figure 1. Both devices work from their own local copy first; while connected, changes pass to the other device through Firebase. Every event and type is a separate document in the cloud.

Trap 1: Writing the whole calendar as one document

The app's first version stored the whole calendar as a single document with a “last updated” time; when two copies met, the newer one won. On one device that is fine. On two devices this happens: I add an event on the phone while it is offline, and meanwhile I add another event on the computer. When the phone reconnects, one copy counts as “newer” and overwrites the other entirely. An event disappears without any warning.

First version: the whole calendar as one document

  1. Phone offline: adds event A.
  2. Computer: adds event B and writes it to the cloud.
  3. The phone reconnects; one copy counts as “newer”.

✗ Lost:One copy overwrites the other entirely; A or B disappears without warning.

Now: every event its own document

  1. Phone offline: adds event A.
  2. Computer: adds event B and writes it to the cloud.
  3. The phone reconnects; A and B are separate documents.

✓ Kept:Both A and B stay. Only if the same event changes in two places does the latest change apply.

Figure 2. The same events with two different units of merging.

The fix was to make the unit of merging smaller. Every event and every event type is now its own document (users/{uid}/events/{id}), each carrying its own change time. The rule is “last change wins” per item: if two devices change different events, both are kept; if they change the same event, the latest change applies. Even the order of event types is stored as its own field so that it can sync too.

Trap 2: Deleted items come back

When you delete an event, deleting its cloud document as well seems natural. But if the other device is offline at that moment, when it returns it sees: “I have this event and the cloud doesn't; it must be new, let me upload it.” The deleted event comes back.

Deleting the document entirely

  1. The computer deletes event C; its cloud document is deleted too.
  2. The phone is offline at the time and still has C.
  3. The phone reconnects: “I have C, the cloud doesn't.”

✗ Comes back:The phone takes C for new and uploads it again.

Leaving a tombstone

  1. The computer deletes event C; only “deleted” and its time stay in the cloud.
  2. The phone is offline at the time and still has C.
  3. The phone reconnects and sees the tombstone.

✓ Deleted:The phone deletes C as well. If C was changed on the phone after the deletion, that change wins.

Figure 3. A deleted event coming back from a device that was offline, and a tombstone preventing it.

I solved this with two things. A deleted item's document stays, but only carries a “deleted” flag and a time (a tombstone). And every device keeps its own copy of the last cloud state it knew. That way it can tell whether an item that “is in the cloud but not here” is new, or was deleted by me.

Trap 3: An empty cache looks like an empty cloud

This was the sneakiest case I noticed while designing. When Firestore starts listening to a list, it first answers from its local cache; the server's answer comes later. On a new device that has never synced, the cache is empty. If I read that first answer as “there is nothing in the cloud”, the new device treats its default event types as “local changes” and writes them over the types in the account.

The rule became: a device that has never synced writes nothing until a real answer from the server has arrived. A device that has synced before keeps working when it opens offline, because it already knows the cloud state.

  1. 1. Listening startsA new device has signed in.
  2. 2. Cache answerEmpty. A device that never synced has nothing cached.
  3. 3. Server answerThe account's real events and types.

✗ Writing at 2:The empty answer looks like “the cloud is empty”; the device's default types are written over the account's.

✓ Waiting for 3:The account's data comes first; then only what really changed is written.

Figure 4. Firestore's two answers on a new device, and after which one it is safe to write.

How did I test it?

I used Firebase's test server that runs on the computer (the Emulator Suite) and opened the app in two windows with completely separate storage: one “phone” and one “computer”. The cases I checked:

Figure 5. Test results on the local Firebase test server. Each bar shows the share of attempts with the expected result; details in the table below.
CaseResult
Sign-up, sign-in, add, edit, complete, delete, two simultaneous adds, type change, reopening (11 checks)11 / 11
Phone offline: a new event, completing an event, renaming the “Market” event. Meanwhile on the computer: a new event and renaming the same “Market” laterAll merged; the computer's “Market” version won
Reading / writing another user's data, reading while signed out, writing to a list that is not allowed (4 attempts)4 / 4 rejected (403)

The test checked every 250 milliseconds; changes appeared on the other “device” within that interval or right after it. That number belongs to a local test server; over the real internet the delay will be longer. One small lesson too: my first offline test was invalid, because Electron's “go offline” switch did not cut the connection to the local test server. Simulating the outage with Firestore's own “disable network” call made the test meaningful.

Known limits

Three questions for your own app

Transparency

I write the code for this app with AI, using Claude Code. Which approach to take, what to test and what to publish are my decisions. All results here were taken on my computer, on Firebase's test server; they are not measurements over the real internet. The sync code is in the zaman-planlayici repository: src/sync.js, firestore.rules for the security rules, and the test cases with their output in docs/senkron-testleri.md (in Turkish).

References