r/swift • • Oct 15 '26

I managed to implement SwiftData + CloudKit Sharing in my app

Hi everyone! I saw a few discussions about SwiftData that don’t support CloudKit sharing for collaboration yet. I was myself searching for a solution after building my app around SwiftData and when I wanted to add collaboration functionality for my shopping list & grocery management app I discovered that SwiftData don't support sharing like CoreData does, I searched everywhere for a solution and the only solution from people here was to move from SwiftData and switch to core data but I really liked SwiftData and I didn't wanted to change it since it was well implemented with my app written in SwiftUI.

So after a lot of research and tests I managed to make collaboration work as well as syncing the private db perfectly with SwiftData using CKSyncEngine. So if you are also in this place where you have or want to build a SwiftData app and implement collaboration know that you can make it work!

My solution was to use two CKSyncEngine isolated in an Actor, one for the private data and the second for the shared data and a database manager that receives the updates from the engines and writes to the database.

It requires some work to start building it, you'll need to well understand CloudKit and a bit of Swift concurrency but once your first prototype works its pretty straightforward forward from there.

I'd be happy to hear your suggestions or learn how you'd approach it differently and if you have questions i'll be happy to answer them. hop this post can be useful!

Link to my app if you want to try:

Kaeli
https://apps.apple.com/il/app/kaeli/id6756275182

1 Upvotes

15 comments sorted by

4

u/__markb Oct 15 '26

I recently did the same in my app and even made it into a package: https://github.com/markbattistella/SwiftDataSync

Then in my app ( https://apps.apple.com/us/app/otuli-shared-baby-tracker/id6801689523 ) I ended up being able to use the syncing to have a portal leveraging CloudKitJS.

Now I have SwiftData driving everything, synced on iCloud via the CKSyncEngine, but also users can use the browser to access it all too.

1

u/Apprehensive-Bet2844 Oct 16 '26

Amazing! That sounds really interesting, I will dive in a bit to see more. That a huge challenge to make a package that could fit completely different apps

2

u/Financial_app Oct 21 '26

One case I'd add to the offline tests: device A deletes an item while device B edits that same item offline, then B reconnects. After syncing and relaunching both devices, does the item stay deleted, or can B's queued edit bring it back? Curious how you've chosen to resolve delete-versus-edit, since the duplicate-add case is already covered in the replies.

1

u/Apprehensive-Bet2844 28d ago

Sorry for the late answer. That’s an interesting one because there’s no perfect solution for this, here I did the last edit to win. then if user1 removed the product offline and then user2 edit the product it will be back on user1 when he’s back online

2

u/Comprehensive-You522 Oct 16 '26

Nice approach, keeping SwiftData and putting CKSyncEngine behind an actor sounds a lot cleaner than migrating to Core Data. How do you handle two people editing the same item on a shared list at the same time? With CKSyncEngine you get the server record back on a conflict, so I'm curious if you merge field by field or let the last write win.

1

u/Apprehensive-Bet2844 Oct 16 '26

Good point! When a product is edited then yes I let the last write win. In the situation where two users add the same product to the shopping list (and each product must be uniq in the shopping list) then I compare the IDs of the element, keep the first one created and delete the last one.

1

u/Comprehensive-You522 Oct 16 '26

Keeping the oldest one makes sense. Does that cleanup run on every device when the change syncs, or only on the one that sees the duplicate first? I'd check what happens when two phones both go offline, add the same product, and then sync at the same time.

1

u/Apprehensive-Bet2844 Oct 16 '26

That works even if the 2 devices are offline and then sync together, because the creation date is saved on device so when you start to sync them together each device send its copy of the product and then the earlier is kept

1

u/launchapp_indie_dev Oct 16 '26

Wait, but if both phones were offline when creating it, they generated different record IDs, right? So CloudKit will just deliver both records to both devices anyway.

Do you run a local check comparing product names after fetching to find duplicates and delete the newer one? Also curious if device clock skew ever messes up that "earlier date" comparison for you? 

2

u/Apprehensive-Bet2844 Oct 16 '26

The device compare the underlying product id (the product in the shopping list is another entity) so in other words: If this product is already in the shopping list then keep only one.

And about changing the device clock even if a user do it, it will not have a meaningful consequence because only one of the two product will be chosen anyway.

1

u/launchapp_indie_dev Oct 16 '26

Ah, having a separate product entity with its own ID makes total sense, that dodges the free-text collision completely. And true, for a grocery list it  doesn't matter which one wins as long as it's on the list.

Thanks for explaining, really neat approach!

2

u/kokerali Oct 16 '26

How are you handling iCloud account changes in the SwiftData store? Apple documents that CKSyncEngine resets its own state, including pending changes, but updating local persistence is still the app's responsibility: https://developer.apple.com/documentation/cloudkit/cksyncengine-5sie5/event/accountchange

One test I'd add: edit a list offline under account A, quit the app, switch to an unrelated account B with no access to that list, then relaunch. Check that A's rows aren't shown to B, and that any app-owned upload queue doesn't reschedule A's edits into B's engine. That's separate from the pending changes CKSyncEngine already clears. If you retain local-only drafts, keeping those separate from account-backed records would make that policy easier to reason about. Have you tested that path with both engines?

1

u/Apprehensive-Bet2844 Oct 16 '26

Whenever the account change or log out I can catch this event from the engine and when I fetch it I simply delete all the data from the data store.

1

u/caffeinated1337 Oct 16 '26

If you haven't already, save the stateSerialization from the .stateUpdate event and hand it back in the configuration when you create each engine. Without it the engine starts from scratch and refetches everything on every launch.

The shared engine also needs to handle zone deletions in .fetchedDatabaseChanges. That's how you find out the owner stopped sharing or someone left the list, and nothing removes those local records for you.

1

u/Apprehensive-Bet2844 Oct 16 '26

Yes, Apple explicitly says that the state of the engine should be persisted locally.