NSPersistentCloudKitContainer & persisting Core Data after removing a share

442 views Asked by At

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?

1

There are 1 answers

0
DIV On

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.