ios - Parsing CloudKit Error (CKError)
问题描述
I'm using CloudKit and I'm checking if a specific zone was already created.
For this example, let's say that a zone isn't set, so CloudKit retrieves me a CKError
.
This CKError
has a property called partialErrorsByItemID
which is of type [AnyHashable : Error]?
Here's the code:
fileprivate func checkIfZonesWereCreated() {
let privateDB = CKContainer.default().privateCloudDatabase
let op = CKFetchRecordZonesOperation(recordZoneIDs: [zoneID1, zoneID2])
op.fetchRecordZonesCompletionBlock = { (dict, err) in
if let err = err as? CKError, let _err = err.partialErrorsByItemID {
print(_err)
/* prints
[AnyHashable(<CKRecordZoneID: 0x60800003cba0; ownerName=__defaultOwner__, zoneName=TestZone>): <CKError 0x60400005a760: "Zone Not Found" (26/2036); server message = "Zone 'TestZone' does not exist"; uuid = ...-2DF4E13F81E2; container ID = "iCloud.com.someContainer">]
*/
// If I iterate through the dictionary
_err.forEach({ (k, v) in
print("key:", k) // prints: key: <CKRecordZoneID: 0x60800002d9e0; ownerName=__defaultOwner__, zoneName=TestZone>
print("value:", v) // prints: value: <CKError 0x60400005a760: "Zone Not Found" (26/2036); server message = "Zone 'TestZone' does not exist"; uuid = ...-2DF4E13F81E2; container ID = "iCloud.com.someContainer
})
return
}
print("dict:", dict)
}
privateDB.add(op)
}
How do I parse this error? I need to access the zoneName
?
解决方案
The key in _err
is a CKRecordZoneID
. Once you have that, use the zoneName
property to get the zone name.
I would write your code as follows:
fileprivate func checkIfZonesWereCreated() {
let privateDB = CKContainer.default().privateCloudDatabase
let op = CKFetchRecordZonesOperation(recordZoneIDs: [zoneID1, zoneID2])
op.fetchRecordZonesCompletionBlock = { (dict, err) in
if let err = err as? CKError {
switch err {
case CKError.partialFailure:
if let _err = err.partialErrorsByItemID {
for key in _err.keys {
if let zone = key as? CKRecordZoneID {
let name = zone.zoneName
print("Missing zone: \(name)")
}
}
return
}
default:
break
}
}
print("dict:", dict)
}
privateDB.add(op)
}
推荐阅读
- javascript - 获取数组中动态生成的表单数据
- optimization - 如何加快我的近似自然对数函数?
- html - 在引导程序 3.3 和 jquery 1.1 中单击后,折叠按钮显示但不起作用
- ffmpeg - 在 anaconda 中安装包时出错:尝试为未知包类型制作前缀记录
- kubernetes - 如何在转发到认证服务器之前过滤认证请求?
- php - 如何在 TYPO3 7.6 中实现扩展后端部分的控制器
- python-3.x - 如何加快验证素数的python程序?
- javascript - 重复一个元素,直到它覆盖整个屏幕
- reactjs - 我正在寻找一种方法来修复 react material-ui 中表组件中的第一列
- cluster-computing - Ceph 时钟偏差