iosswifterror-handlingcloudkitckerror

recovering errors with CKPartialErrorsByItemIDKey


After getting .partialFailure CKError, I've been trying to recover id's and corresponding errors but I'm having problems...

Right now I'm using:

print("pE \(error.partialErrorsByItemID) or \(error.userInfo[CKPartialErrorsByItemIDKey])")

    if let dictionary = error.userInfo[CKPartialErrorsByItemIDKey] as? [NSObject: Error] {

print("partialErrors #\(dictionary.count)")    // <-- Not reaching this...

I've also tried the following:

if let dictionary = error.partialErrorsByItemID {   // <-- error.pEBIID returns nil

and:

if let dictionary = error.userInfo[CKPartialErrorsByItemIDKey] as? [CKRecord : CKError /* and Error */] {    // <-- but neither triggers the if-let

The first print is showing this in the console (I switched the open tags leftward so that they wouldn't be interpreted as html):

pE nil or Optional({
">CKRecordID: 0x7b95ace0; CentralTableView:(_defaultZone:__defaultOwner__)>" = ">CKError 0x7a7e4cf0: \"Server Record Changed\" (14/2004); server message = \"record to insert already exists\"; uuid = B7AD7528-D8AE-4DCB-91FF-16B5271110F5; container ID = \"iCloud.com.yadayadayada\">";
})

As I understand from the documentation I should be getting a NSDictionary<CKRecordID, (CK)Error> back from userInfo dictionary with CKPartialErrorsByItemIDKey and a NSDictionary<NSObject, Error> from the partialErrorsByItemID method. Based on the first print, the method isn't working in this situation but key is giving me a dictionary of CKRecordID and CKError. I don't see why the second print isn't getting reached?


Solution

  • According to the documentation, you get back an NSDictionary, not a Swift dictionary.

    Try:

    if let dictionary = error.userInfo[CKPartialErrorsByItemIDKey] as? NSDictionary {
        print("partialErrors #\(dictionary.count)")
    }