r/swift • u/Apprehensive-Bet2844 • 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:
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.
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.
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.
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/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.