I want to put excellent CloudKit error handling in my app as Apple wants us to do. I want to save and modify a record right now. Here is my basic save logic...
func addNewRecord(managedObj: NSManagedObject) {
let newRec = managedObj.convertToCkRecord()
publicDB.saveRecord(newRec, completionHandler: saveHandler)
}
func saveHandler(savedRecord: CKRecord?, error: NSError?) {
// handle errors here
if let error = error {
if error.code == CKErrorCode.NotAuthenticated.rawValue {
// debug
print("Not authentricated")
}
else if error.code == CKErrorCode.NetworkFailure.rawValue {
print("Network failure!!")
if let retryAfterValue = error.userInfo[CKErrorRetryAfterKey] as? NSTimeInterval {
let delayTime = dispatch_time(DISPATCH_TIME_NOW, Int64(retryAfterValue * Double(NSEC_PER_SEC)))
dispatch_after(delayTime, dispatch_get_main_queue()) {
// THIS IS WHERE I GET STUCK, WHERE DO I FIND THE FAILED CKRECORD FOR RETRY?
// IS IT IN USERINFO SOMEWHERE?
//self.saveHandler(savedRecord, error: error)
}
}
}
}
else {
print("Save was a success! \(savedRecord)")
}
}
This seems it should be basic, but every example I see about this just has a comment //retry or //handle error, including in the WWDC tutorials where the error handling should go. What I want to know is how do I find a reference to my failed CKRecord? Storing it in a local variable seems like it won't work because of the multithreading issue. I tried adding it to a queue, but other threads could get to that queue as well so I worry about race conditions.