CloudKit CKdatabaseOperation 不会根据 .qualityOfService 设置在模拟器中抛出错误

CloudKit CKdatabaseOperation does not throw error in simulator depending on .qualityOfService setting

我基本上是在搞乱CloudKit。我有 2 个问题想 help/education 回答。

(1) 当我尝试在 关闭 WiFi 的模拟器中 运行 以下代码时 (模拟网络不可用),我希望它抛出网络不可用的错误。但是,cloudKit 不会抛出任何错误,函数也不会执行任何操作,即不会执行 modifyRecordZonesCompletionBlock

中的任何一个打印语句
self.container = CKContainer.default()
self.privateDB = self.container.privateCloudDatabase
let createZoneGroup = DispatchGroup()

createZoneGroup.enter()

let createZoneOperation = CKModifyRecordZonesOperation(recordZonesToSave: [customZone], recordZoneIDsToDelete: [])

createZoneOperation.modifyRecordZonesCompletionBlock = { (saved, deleted, error) in
if let theerror = error {
// Need to handle error
    print("Create Custom zone error \(theerror)")
} else {
    print("Custom Zone created")
}
    createZoneGroup.leave()
}

createZoneOperation.qualityOfService = .background
self.privateDB.add(createZoneOperation)

但是,当我将 createZoneOperation.qualityOfService 更改为 defaultuserInitiated 时,它会抛出错误(如下所示),这是预期的行为。为什么是这样?这只是模拟器异常还是云套件错误的抛出与服务的操作质量有关,为什么?我无法在 cloudkit API 参考资料中找到有关此行为的任何信息。

Create Custom zone error <CKError 0x60800005a2e0: "Partial Failure" (2/1011); "Failed to modify some record zones"; partial errors: {
    Main:__defaultOwner__ = <CKError 0x610000242910: "Network Unavailable" (3/NSURLErrorDomain:-1009); "The Internet connection appears to be offline.">
}>

(2) 另外,我预计它会直接抛出 Network Unavailable 错误,而不是 Partial Failure 失败错误,其中包含 Network Unavailable 错误。这是预期的行为吗?它是否总是抛出 Partial Failure 错误,然后必须解包才能找到包含的错误消息?文档似乎非常缺乏任何解释,除非我没有找到正确的地方。

非常感谢任何帮助。谢谢。

我试着回答这两个问题...

网络

没有模拟器错误或问题,您没有收到错误连接,因为 .background 服务质量意味着...

The system performs work initiated by a user sooner than background work that can be deferred until a more optimal time.

也许这就是您没有收到错误的原因,这是因为系统正在等待更好的网络条件,而您设置 .default 或 .userInitiate 系统期望“实时”响应。

引自Energy Efficiency Guide for iOS Apps

要更好地模拟网络条件,请使用 Apple 的 Network Link Conditioner 工具。 Here you could find more info关于它。

部分错误

我不确定,但我认为部分失败是所有 CKxxxxxOperation 错误结果的通用包装器。

这是有道理的,因为这些操作涉及 1..N 个 CKRecords,其中一些可以正常完成,而另一些可能会出错