检查 CloudKit 数据库操作是否正在进行

Checking if a CloudKit database operation is in progress

如何检查 CKDatabase 是否已经在进行中 CKQueryOperation

我想获取一些 CloudKit 记录,但我可能会多次提交请求。举例说明:

let container = CKContainer(identifier: "com.example.app")
let publicDB = container.publicCloudDatabase

// somehow determine if we have an already in progress operation here
if (inProgress) {
   return
 }

let query = CKQuery(recordType: "MyRecord", predicate: NSPredicate(value: true))
let operation CKQueryOperation(query: query)
// Omitting completion block for brevity
publicDB.add(operation)

遗憾的是,无法获取有关 Cloud Kit 数据库操作队列操作的信息。

一种解决方案是避免使用 CKDatabase add: 方法。相反,创建您自己的 OperationQueue 并将您的数据库操作添加到此队列,而不是使用 CKDatabase add:.

然后您可以检查您的操作队列中是否有任何操作。

确保在将操作添加到您自己的队列之前设置数据库操作的 database 属性。