I need help to learn how to handle errors when retrieving records through CloudKit . I currently have an application that saves a lot of records in the cloud and downloads them at startup. I reference records using CKReference , and anytime I save the link, I use the CKReferenceAction.DeleteSelf parameter. The problem I encountered periodically is that when a deleted record is deleted, it can sometimes take a considerable amount of time before the link removes itself. This made me sometimes confronted with a situation where my application chose CKReference for a record that no longer exists. I can find out when this happens by simply inserting print(error!) Into the error handler. I would like to know how I can add code to detect this particular error, i.e. if error.localizedDescription == ??? { if error.localizedDescription == ??? { . Here is the basic code that I use to extract:
let fetch = CKFetchRecordsOperation(recordIDs: recordIDs) fetch.perRecordCompletionBlock = { (record:CKRecord?, recordID:CKRecordID?, error: NSError?) in if error != nil { // Error Line A (See below) print("ERROR! : \(error!.localizedDescription)") // Error Line B (See below) print("ERROR: \(error!)") } else if let record = record { // Record was found } } if let database = self.privateDatabase { fetch.database = database fetch.start() }
And then, when he tries to retrieve a nonexistent entry, here is the error message that appears in the compiler window:
a) ERROR! : Error fetching record <CKRecordID: 0x10025b290; dbbda7c3-adcc-4271-848f-6702160ea34f:(_defaultZone:__defaultOwner__)> from server: Record not found ERROR! : Error fetching record <CKRecordID: 0x10025b290; dbbda7c3-adcc-4271-848f-6702160ea34f:(_defaultZone:__defaultOwner__)> from server: Record not found
b) ERROR: <CKError 0x125e82820: "Unknown Item" (11/2003); server message = "Record not found"; uuid = (removed); container ID = "(removed)"> ERROR: <CKError 0x125e82820: "Unknown Item" (11/2003); server message = "Record not found"; uuid = (removed); container ID = "(removed)">
Above, on error line B, where it says CKError 0x125e82820: can I use this to create an if statement to check for this particular type of error? I really could use any help to find a way to properly solve this problem when this happens. I created some loading structure for my application, and when he thinks that there is a record that he should find, but cannot, this closes my loading process. I would really appreciate any help I can get, I assume this is a simple solution, but apparently this is not what I could find. Thanks!
UPDATE -
Thanks to @AaronBrager, I was able to find the right solution. You can check the error code to match any specific error, and the domain to make sure it is a CKError . Here is a solution that works for me:
let fetch = CKFetchRecordsOperation(recordIDs: recordIDs) fetch.perRecordCompletionBlock = { (record:CKRecord?, recordID:CKRecordID?, error: NSError?) in if error != nil { if error!.code == CKErrorCode.UnknownItem.rawValue && error!.domain == CKErrorDomain { // This works great! } } else if let record = record { // Record was found } } if let database = self.publicDatabase { fetch.database = database fetch.start() }