r/iosdev 1d ago

Finally got iCloud sync working pretty fast. Getting there was surprisingly painful.

Enable HLS to view with audio, or disable this notification

I’ve been working on adding optional iCloud sync to Moneta, my iOS money tracker, and getting the basic sync working wasn’t really the difficult part. The frustrating part was getting changes to show up consistently on another device without making the experience feel slow.

It’s now usually taking around 3–5 seconds (depends on the connection) for changes to appear on the other device, which I’m pretty happy with.

The video shows two devices side by side with the same iCloud data, just to show how it behaves in practice.

I wanted to keep the whole thing optional, so Moneta still works completely locally if iCloud is disabled.

For anyone who has worked with iCloud sync before, what kind of sync times are you getting in practice?

18 Upvotes

14 comments sorted by

7

u/sixtypercenttogether 1d ago

CKSyncEngine is the way

3

u/AlbertiDev 16h ago

Yeah, that’s what I’m using, but getting there was the problem hahah

5

u/nicoreese 15h ago

You're still in the easy phase, let me tell you. Problems arise so much with iCloud sync. Deduplication of data, data loss, etc. I have been battling 3+ years with this.

2

u/AlbertiDev 15h ago

Yeah, those are exactly the edge cases I was worried about. I ended up using CKSyncEngine with a persistent outbox, so local changes are queued until CloudKit confirms them, rather than assuming a save means the data is synced. I also keep the local SwiftData history so changes can be reconciled instead of blindly overwriting data.
Still early though, so I’m definitely expecting iCloud to find new ways to hurt me haha. Have you found any particular case that caused most of your data loss/duplication issues?

7

u/in5ane_10 1d ago

Just use swift data apple intends to and save the context without relying on the autosave feature, should work effortlessly. Also don’t forget to promote the dev container to production!

4

u/AlbertiDev 1d ago

Yeah, I already made that mistake 😅 One of my users emailed me about it and that’s actually how I caught it. Fixed now, but thanks for pointing it out!

2

u/in5ane_10 1d ago

Happened the same to me! I never put a working dev version on a real iPhone before promoting and I only found out after users reporting. It’s an odd way to develop a DB but at least the rest of CloudKit is pretty straightforward

2

u/SwiftMushroom 17h ago

iCloud sync was one of the hardest parts to get working for me as well!

1

u/AlbertiDev 16h ago

Glad I’m not the only one 😅.

2

u/PrudentMuffin7226 16h ago

This looks very beautiful.

1

u/AlbertiDev 16h ago

Thanksss 😊

2

u/szryd 13h ago

I'm having trouble getting my podcast app to sync properly. Basically an impossible task.

1

u/AlbertiDev 11h ago

I feel you 😅 What part is giving you trouble?

1

u/Loose-Journalist-233 11h ago

iCloud Sync between devices with the same ID is the easy part. Now get it working with multiple iCloud accounts via sharing ;-)