我正在寻找保存 CKRecord
时正确处理错误的示例。根据 Apple docs我应该“使用错误对象中的信息来确定问题是否有解决方法。”
我知道错误对象有一个 userInfo
字典,但我如何确定字典的键是什么以及如何处理错误?
以下示例说明了我当前如何保存 CKRecord
:
CKRecord *record = [[CKRecord alloc] initWithRecordType"MyRecordType"];
[record setValue:[NSNumber numberWithInt:99] forKey"myInt"];
[db saveRecord:record completionHandler:^(CKRecord *savedPlace, NSError *error) {
// handle errors here
if (savedPlace) {
NSLog(@"save successful");
}else{
NSLog(@"save unsuccessful");
}
if (error) {
NSLog(@"Error saving %@", error.localizedDescription);
}
}];
如何改进此代码以解决潜在的保存问题?
在我的图书馆EVCloudKitDao我有一个单独的方法,它将根据错误代码返回错误类型。根据该类型,您可以决定要做什么。这是那个方法:
public enum HandleCloudKitErrorAs {
case Success,
Retry(afterSecondsouble),
RecoverableError,
Fail
}
public static func handleCloudKitErrorAs(error:NSError?, retryAttemptouble = 1) -> HandleCloudKitErrorAs {
if error == nil {
return .Success
}
let errorCode:CKErrorCode = CKErrorCode(rawValue: error!.code)!
switch errorCode {
case .NetworkUnavailable, .NetworkFailure, .ServiceUnavailable, .RequestRateLimited, .ZoneBusy, .ResultsTruncated:
// Use an exponential retry delay which maxes out at half an hour.
var seconds = Double(pow(2, Double(retryAttempt)))
if seconds > 1800 {
seconds = 1800
}
// Or if there is a retry delay specified in the error, then use that.
if let userInfo = error?.userInfo {
if let retry = userInfo[CKErrorRetryAfterKey] as? NSNumber {
seconds = Double(retry)
}
}
NSLog("Debug: Should retry in \(seconds) seconds. \(error)")
return .Retry(afterSeconds: seconds)
case .UnknownItem, .InvalidArguments, .IncompatibleVersion, .BadContainer, .MissingEntitlement, .PermissionFailure, .BadDatabase, .AssetFileNotFound, .OperationCancelled, .NotAuthenticated, .AssetFileModified, .BatchRequestFailed, .ZoneNotFound, .UserDeletedZone, .InternalError, .ServerRejectedRequest, .ConstraintViolation:
NSLog("Error: \(error)")
return .Fail;
case .QuotaExceeded, .LimitExceeded:
NSLog("Warning: \(error)")
return .Fail;
case .ChangeTokenExpired, .ServerRecordChanged:
NSLog("Info: \(error)")
return .RecoverableError
default:
NSLog("Error: \(error)") //New error introduced in iOS...?
return .Fail;
}
}
在 CloudKit 方法的回调中,您可以像这样使用这个函数:
func loadContacts(retryCountouble = 1) {
// Look who of our contact is also using this app.
EVCloudKitDao.publicDB.allContactsUserInfo({ users in
EVLog("AllContactUserInfo count = \(users.count)");
Async.main{
self.contacts = users
self.tableView.reloadData()
}
}, errorHandler: { error in
switch EVCloudKitDao.handleCloudKitErrorAs(error, retryAttempt: retryCount) {
case .Retry(let timeToWait):
Async.background(after: timeToWait) {
self.loadContacts(retryCount + 1)
}
case .Fail:
Helper.showError("Something went wrong: \(error.localizedDescription)")
default: // For here there is no need to handle the .Success, .Fail and .RecoverableError
break
}
})
}
在我上面的例子中,我使用了一个单独的错误回调处理程序。您也可以在 CloudKit 方法回调中直接调用它。只需先检查是否有错误。
关于ios - 保存 CKRecord 时的错误处理,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32621306/
欢迎光临 OStack程序员社区-中国程序员成长平台 (https://ostack.cn/) | Powered by Discuz! X3.4 |