My app has three main SwiftData models: Collection, SavedItem, and Extract.
- A Collection can contain subcollections (folders within folders) and SavedItems (files).
- Each SavedItem can have child Extracts.
I'm preparing for the ability for users to be able to share Collections with each other.
Currently, my architecture treats each Collection as the root of its own CloudKit zone (a root parent Collection and all of its items and subcollections live in 1 zone).
This makes sharing and isolation straightforward, but it also means that moving a SavedItem or subcollection between Collections involves moving it across zones.
I’m trying to figure out the best pattern for handling these cross-zone moves while keeping data integrity, relationships, and sharing intact.
My understanding is that in CloudKit, and moving a record from Zone A to Zone B would require deleting it from Zone A and recreating it in Zone B - while somehow maintaining the link back to my local SwiftData store.
Has anyone run into this or know how best I should handle it?
If you are using SwiftData + CloudKit, which is based on NSPersistentCloudKitContainer, it doesn't support cross-share relationships, and so you can't have a collection in one zone and a relationship of the collection (a sub-collection, for example) in another. This is mentioned in here:
" NSPersistentCloudKitContainer doesn’t support cross-share relationships. That is, it doesn’t allow relating objects associated with different shares. When sharing an object, NSPersistentCloudKitContainer moves the entire object graph, which includes the object and all its relationships, to the share’s record zone. "
If you are using the CloudKit framework directly, the source and target objects of CKRecord.Reference must be in the same zone of the same database, and so you will need to maintain the relationship with your own logic.
Best,
——
Ziqiao Chen
Worldwide Developer Relations.