gpt4 book ai didi

ios - 保存 CKRecord 时的错误处理

转载 作者:塔克拉玛干 更新时间:2023-11-02 09:59:53 26 4
gpt4 key购买 nike

我正在寻找保存 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(afterSeconds:Double),
RecoverableError,
Fail
}

public static func handleCloudKitErrorAs(error:NSError?, retryAttempt:Double = 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(retryCount:Double = 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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com