// notes
Sync without a server: what I learned shipping CKSyncEngine in a small notes app
MEMOROLL is a notes app I build mostly on my own, on Amami Oshima, an island in southern Japan. For its first two months it had no sync, on purpose. When I added iPad and Mac apps in version 2.1, I wanted sync without becoming the server: no account, no backend of mine, nothing for me to run or protect. So the app syncs only through the user's own CloudKit private database, using CKSyncEngine.
This is a short write-up of how that turned out: four decisions that worked, and one pitfall that cost me a weekend.
The shape of it
- One custom zone ("Notes") and one record per note. The record name is the note's own ID, so the same note on two devices is the same record.
- The local SQLite database stays the source of truth. Writing a note never waits on the network. The app writes to SQLite first and tells the engine "this record changed"; the engine asks for the record later, and the app builds it from the database at that moment.
- Sync is optional and starts off. While it's off, nothing is sent. Turning it off later keeps every note on the device.
Decision 1: let the database build the record, late
CKSyncEngine asks you for records in nextRecordZoneChangeBatch. I don't keep CKRecords around. I keep only each record's system fields (encoded with encodeSystemFields) next to the row in SQLite, and rebuild the record from the row when the engine asks for it. If the note no longer exists by then, I drop it from the pending list instead of sending a stale copy.
This keeps one rule simple: whatever is in the database is what gets sent.
Decision 2: encrypt the content, leave the ordering in the clear
Note text, place names and coordinates go into encryptedValues. Timestamps and the day key stay as plain fields, because they are what the app sorts and groups by, and they say nothing on their own. Photos are CKAssets, with their relative paths kept in a parallel array so each file lands in the same place on every device.
How much of this is end-to-end depends on the user's own iCloud settings: with Advanced Data Protection turned on, the encrypted fields and assets are end-to-end encrypted, the same as Apple's own Notes. I describe it to users in exactly those terms.
Decision 3: a small, boring conflict rule
When a save fails with serverRecordChanged, the app takes the server's record, merges it into the local row, and sends again on top of the server's version. The merge is deliberately small:
- Text, photos and place: whichever side has the newer
updated_at. - Star, to-do and done marks: keep the local ones (that's the tap the person just made).
- Location is often filled in a few seconds after writing, so if only one side has it, keep it.
Notes are short and usually written on one device at a time, so this has been enough.
Decision 4: send a second after saving
The engine's own automatic sends arrived anywhere from a few seconds to tens of seconds after a save. For a notes app that felt slow, so after each save or delete the app waits one second (restarting the wait if you keep typing) and calls sendChanges() itself. If that fails, the engine's own schedule picks it up later.
The pitfall: receiving depends on pushes
After shipping the TestFlight build, I wrote a note on my iPhone with the Mac app open next to it. The first time, it didn't show up on the Mac for more than three minutes. The second time, thirty seconds.
What I saw in the Mac's logs: in my testing, CKSyncEngine fetched changes promptly when a silent push arrived, and on the days I tested, pushes often didn't arrive for a long time. Calling fetchChanges() myself didn't help in that state; as far as I could tell from the logs, it returned without asking the server when no push had come in. The fetch that did go out happened when the window came to the front.
The fix was to ask the zone directly while the app is in the foreground:
// every 10 s while visible (30 s in Low Power Mode)
let r = try await privateDB.recordZoneChanges(
inZoneWith: zoneID, since: myToken,
desiredKeys: priming ? [] : nil,
resultsLimit: priming ? nil : 200)
A few details that mattered:
- Keep your own change token, separate from the engine's state, and store it next to the data.
- The first call has no token. Ask for no keys (
desiredKeys: []) and only save the token, so the first poll doesn't download every note and photo again. - Apply what comes back through the same code path the engine uses. The engine will often deliver the same change again later; since applying is "write these values", doing it twice is harmless.
- On
changeTokenExpired,zoneNotFoundoruserDeletedZone, clear the token and start again from "now". - Stop polling when the app goes to the background (and on the Mac, when the window is hidden). When nothing has changed, the request is small.
With pushes, changes still appear almost at once. Without them, it's about ten seconds.
What I'd tell myself two months ago
- Decide what "the truth" is first. For me it's the local database, and sync is a copy that catches up.
- Don't assume pushes arrive. Test with two devices side by side, on more than one day.
- Keep the merge rule small enough to explain in a sentence.
I'd still like to hear from anyone who has shipped CKSyncEngine: is foreground polling the right workaround, or is there a cleaner way I missed?
MEMOROLL is free on the App Store for iPhone, iPad and Mac: https://apps.apple.com/app/id6811861479 Why a notes app built without sync now has it: https://resdev.jp/en/notes/why-sync-now/
Hideyuki Sakamoto, RESONANCE, K.K. — Amami Oshima, Japan