0
votes

According to Apple docs here

https://developer.apple.com/documentation/coredata/mirroring_a_core_data_store_with_cloudkit/reading_cloudkit_records_for_core_data

You can get a CKRecord from a NSManagedObject using NSPersistentCloudKitContainer.

Access CloudKit Objects You can access a managed object’s CKRecord directly through its associated context using record(for:) for a single record, or records(for:) for multiple records. To retrieve the record ID only, use recordID(for:), or recordIDs(for:). Alternatively, use the class functions record(for:), records(for:), recordID(for:), and recordIDs(for:) on NSPersistentCloudKitContainer.

But it doesn't say how to get a fresh copy? If you are working with CloudKit Sharing, you are modifying CK without going through CoreData. In which case you have to wait for NSPersistentCloudKitContainer to next fetch for updates.

But if you want to present the latest Sharing Status, you need to query Cloudkit. How to do this?

1

1 Answers

1
votes

Using the fetch with RecordID (below) will let you get the latest record:

func fetch(withRecordID recordID: CKRecord.ID, 
completionHandler: @escaping (CKRecord?, Error?) -> Void)

If you want to fetch it and then update a property and save, you'd do something like:

Get the record from cloudkit

CKContainer.default().publicCloudDatabase.fetch(withRecordID: user.id!) { updatedRecord, error in

Then set the new array for the key and key type I'm saving (where array for user is the thing I've updated and 'keyType' is the CK record field name)

updatedRecord!.setObject(arrayForUser as __CKRecordObjCValue?, forKey: keyType)

Then make the save call, passing the updated record:

        CKContainer.default().publicCloudDatabase.save(updatedRecord!) { savedRecord, error in