NSPersistentCloudKitContainer exclude relationship from share

I am trying to add CloudKit sharing to my app using the new iOS 15 share method https://developer.apple.com/documentation/coredata/nspersistentcloudkitcontainer/3746834-share

In the app there are 3 core data entities (Folder, Item, Comment) with the following relationships:

A Folder contains many Items

An Item has many Comments

I want to use CloudKit to share just the Item entity, not any of its relationships. Is this possible to do with the share(_:to:completion:) method?

Currently, when I pass an Item to the share method it also includes the Folder and Comments in the CKShare. How do I prevent this?

Replies

hi,

... this may not directly answer your CloudKit sharing, but what you may be asking is how to place all Core Data Folder and Comment objects into a local persistent store configuration, and allItem objects into a cloud configuration that is shared with iCloud.

if that's the case, you cannot implement direct relationships between configurations, so then you might:

  • in every Item, store the id: UUID of the Folder it belongs to (you do put UUIDs into your Core Data entities, yes?) ... sort of a parentID, and
  • in every Comment, store the id: UUID of the Item it belongs to, again, essentially a parentID.

you can then finesse the relationships, say for an Item, finding its parent Folder by doing a Core Data fetch for Folder objects whose id matches the Item's parentID; and similarly, you can locate the Comments associated with an Item by doing a Core Data fetch for Comment objects whose parentID matches the Item's id.

Also take a look at this Apple Sample project on how to handle separate stores in Core Data.

hope that helps,

DMG