0

I'm using NSPersistentCloudKitContainer to sync CoreData+Cloudkit. I am basing my code on Apple's example in the code downloaded (see reference below). My Core Data root record is a hierarchy (list with children items on the list). I have assumed the root record CKShare is being shared - and not the zone (because the share has a hierarchy). However, after reading Apple forums, it seems there may be issues with NSPersistentCloudKitContainer and the answer appears to be to make a "deep copy".

The issue is that when a participant removes themselves from the shared list, or the owner stops the share, the share of course is removed BUT... the list is also deleted from Core Data. It would seem to me (like in the case of Apple's Notes, Reminders, Photos etc), that when a share is removed, the local data should persist (in Core Data).

COREDATA / CLOUDKIT SHARING CODE: (can be downloaded from Apple) Apple's documentation talks about making a "deep copy" before removing the share. (https://developer.apple.com/documentation/coredata/sharing_core_data_objects_between_icloud_users). But there is no further documentation than the mention.

How (and where) would one create a deep copy so that an owners Core Data entity persists after the share is deleted?

DIV
  • 223
  • 1
  • 2
  • 11
  • `NSPersistentCloudKitContainer` syncs CoreData to CloudKit. Whatever is in CloudKit is present in CoreData, keeps it in sync with no exceptions. When an owner removes a share for an item, does the item get deleted for the owner? I can understand if it does get deleted for the participant who is removed from the share. – user1046037 Dec 06 '22 at 18:34
  • Yes. The item gets deleted for both the owner and the participant. – DIV Dec 06 '22 at 18:59
  • It might be worth filing a bug via the Feedback assistant. In the mean time in the logic where you get to remove the share, make a copy for the owner before deleting it – user1046037 Dec 06 '22 at 20:03
  • IMO `NSPersistentCloudKitContainer` is badly designed, it doesn't use `CKReference` like the Notes app does. – malhal Dec 06 '22 at 21:02
  • The share appears to be removed when the PersistenceController : UICloudSharingController calls ** func cloudSharingControllerDidStopSharing(_ csc: UICloudSharingController) ** I think this is where to deep copy but its unclear how to access the record data (and children) from the share & then access the managedContext to store in CoreData. also... here is the reference for Apple's example code (downloadable) and mention of "deep copying" after removing the share (https://developer.apple.com/documentation/coredata/sharing_core_data_objects_between_icloud_users). – DIV Dec 06 '22 at 21:20

1 Answers1

0

For anyone coming across this... I resolved my issue. I found a great example of a Swift 5.0 deep copy on SO by Alexander Braekevelt which returns a copied NSManagedObject complete with its underlying hierarchical relationships.

April 2021: Stack Overflow - Deep Copy example

I am using it in cloudSharingControllerDidStopSharing, purging records for the share first, then deep copying and saving the returned object back to the context.

DIV
  • 223
  • 1
  • 2
  • 11