gpt4 book ai didi

swift - 如果缺少qualityOFService,则不会调用Cloudkit错误处理

转载 作者:行者123 更新时间:2023-11-30 11:05:35 25 4
gpt4 key购买 nike

我正在尝试处理 Cloudkit 错误。我读了这篇文章:CloudKit - full and complete error handling example但我有几个问题:

1.如果我不设置.qualityOFService,为什么错误处理不起作用? .userInitiated 是否正确,正如帖子中提到的那样,它设置为 .background?另外,我是否必须将其设置为 loadDataAgain()

2.我怎样才能使错误处理可重用,它将接受一个错误和另一个参数(根据 View Controller ) 调用自)?

 @objc func loadData() {
let predicate = NSPredicate(value: true)
let query = CKQuery(recordType: myRecordType.type, predicate: predicate)
query.sortDescriptors = [NSSortDescriptor(key: "creationDate", ascending: false)]
let queryOperation = CKQueryOperation(query: query)
queryOperation.resultsLimit = 5
//if the below line is missing errors will not be handled
queryOperation.qualityOfService = .userInitiated
queryOperation.recordFetchedBlock = { item in
self.array.append(item)
}
queryOperation.queryCompletionBlock = { cursor, error in
if error != nil{
if let ckerror = error as? CKError {
if ckerror.code == CKError.requestRateLimited {
let retryInterval = ckerror.userInfo[CKErrorRetryAfterKey] as? TimeInterval
DispatchQueue.main.async {
Timer.scheduledTimer(timeInterval: retryInterval!, target: self, selector: #selector(self.loadData), userInfo: nil, repeats: false)
}
} else if ckerror.code == CKError.zoneBusy {
let retryInterval = ckerror.userInfo[CKErrorRetryAfterKey] as? TimeInterval
DispatchQueue.main.async {
Timer.scheduledTimer(timeInterval: retryInterval!, target: self, selector: #selector(self.loadData), userInfo: nil, repeats: false)
}
} else if ckerror.code == CKError.limitExceeded {
let retryInterval = ckerror.userInfo[CKErrorRetryAfterKey] as? TimeInterval
DispatchQueue.main.async {
Timer.scheduledTimer(timeInterval: retryInterval!, target: self, selector: #selector(self.loadData), userInfo: nil, repeats: false)
}
} else if ckerror.code == CKError.notAuthenticated {
//present relevant alert with action to reload ViewController
} else if ckerror.code == CKError.networkFailure {
//present relevant alert with action to reload ViewController
} else if ckerror.code == CKError.networkUnavailable {
//present relevant alert with action to reload ViewController
} else if ckerror.code == CKError.quotaExceeded {
//present relevant alert with action to reload ViewController
} else if ckerror.code == CKError.partialFailure {
//present relevant alert with action to reload ViewController
} else if (ckerror.code == CKError.internalError || ckerror.code == CKError.serviceUnavailable) {
//present relevant alert with action to reload ViewController
}
}
}
else{
if cursor != nil {
self.loadDataAgain(cursor!)
}
}
OperationQueue.main.addOperation {
self.tableView.reloadData()
}
}

func loadDataAgain(_ cursor: CKQueryOperation.Cursor) {
let queryOperation = CKQueryOperation(cursor: cursor)
queryOperation.resultsLimit = 5

queryOperation.recordFetchedBlock = { item in
self.array.append(item)
}
queryOperation.queryCompletionBlock = { cursor, error in
if error != nil{
//have the same error handling as above
}
else{
if cursor != nil {
self.loadDataAgain(cursor!)
}
}
OperationQueue.main.addOperation {
self.tableView.reloadData()
}
}
Database.share.publicDB.add(queryOperation)
}

最佳答案

关于您的第一个问题,请参阅 this answer这表明不会报告较低服务质量设置的错误:

it's because [the] system is waiting for a better network condition, while [if] you set .default or .userInitiated the system expects a "real time" response

如果您查看Apple's Energy Efficiency Guide for iOS Apps documentation在标题为“关于 CloudKit 和服务质量”的部分中,Apple 似乎证实了这种方法,并表示

CKOperation is a subclass of the NSOperation class. Although the NSOperation class has a default QoS level of NSQualityOfServiceBackground, CKOperation objects have a default QoS level of NSQualityOfServiceUtility. At this level, network requests are treated as discretionary when your app isn’t in use. On iPhones, discretionary operations are paused when Low Power Mode is enabled.

如果您不想要默认的 .utility,则需要为您创建的每个 CKOperation 显式设置不同的 QOS。如果您发现自己经常使用相关操作执行此操作,我建议您查看 CKOperationGroup 及其 default configuration property - 如果您在配置中设置 QOS,它将覆盖组中任何 CKOperation 中的默认值。

关于swift - 如果缺少qualityOFService,则不会调用Cloudkit错误处理,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52776986/

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