2

NSPersistentCloudKitContainer automatically creates all CKRecords for NSManagedObjects, so...

How to get access to this process and set CKRecord.parent before changes will be synched.

I need each CKRecord of type B to have parent record of type A, but avoid fully manual handling of setParent(:) as fully separate mechanism.

bodich
  • 1,708
  • 12
  • 31

1 Answers1

1

After you save to Core Data, you can get the CKRecord with the functions provided by NSPersistentCloudKitContainer read here.

Than you can set parent to record and save it to database with CKModifyRecordsOperation for example, yes you must use CloudKit operations for this to work.

Stamenkovski
  • 959
  • 1
  • 6
  • 10
  • I’ve created such mechanism, it appeared to be overcomplicated. Here is why: 1. I create NSManagedObject (no CKRecord for object yet) 2. Save context (CKRecord appeared only if internet available!, otherwise no CKRecord. 3. Even if internet available, Mirroring is already started sync right in the moment context saved 4. Here my CKModifyRecordsOperation launches – bodich Jul 27 '20 at 05:21
  • If internet is available, fail of this approach is about 5%. If no, I’ve created code that is fetching all objects with “hasRemoteParent” property (I’ve created) and process them. I launch that in NWMonitor handler. Mirroring is doing the same and if I fetch context when internet restored, CloudKit mirroring just breaks and I don’t see internet anymore. It’s just a nightmare, I could not beat that Mirroring fail and started creating sync fully from scratch (anyway I have to do that for sharedCloudDatabase. – bodich Jul 27 '20 at 05:21
  • Well yes it is complicated but if no internet is available you can listen for network changes until the app is running, once internet is available you can sync to shared database. – Stamenkovski Jul 27 '20 at 06:59
  • As I said in the above comment, I already do so. I listen internet with NWPathMonitor (I made mistake, typed NWMonitor). When I do context fetch on internet restore, Mirroring just dies (have no f....g idea why). I can do any operations with CloudKit, but Apple's Mirroring never sync since this fail until restart an app, and says "probably no internet". So, everything you proposed is already done, working, but causes that stupid bug with mirroring fail. Thanks anyway for your time, I am happy to know about new ideas is any, but I've tried everything here I think. – bodich Jul 27 '20 at 08:02
  • This is really weird, I don't know why is this happening. I never experienced this in my apps.. – Stamenkovski Jul 27 '20 at 08:08
  • Yep, I've tried even timers. But once fetching simultaneously with Mirroring — bang! Mirroring stuck forever! You just cannot predict it. It's using its own created context and no control over it, so I cannot queue context. I will do that in my own sync. – bodich Jul 27 '20 at 09:58