如何使用 CloudKit 通过 CKQueryOperation 迭代查询直到游标为零?
How do I use CloudKit to query iteratively with CKQueryOperation until cursor is nil?
我如何声明一个从 CloudKit 中检索记录的闭包,该闭包会迭代调用自身直到获取所有记录?下面的代码就是这样做的,除了我收到一条错误消息
Variable used within its own initial value
在我将闭包 queryCompletionBlock 分配给 属性 的行中,在闭包本身的声明中使用与查询操作相同的名称。基本上,我得到一个错误,因为我在声明闭包之前在闭包声明中调用了闭包本身。
let predicate = NSPredicate(value: true)
let query = CKQuery(recordType: DatabaseNameStrings.recordTypeEntry, predicate: predicate)
let queryOperation = CKQueryOperation(query: query)
queryOperation.desiredKeys = [DatabaseNameStrings.fieldNameCreatedAt, DatabaseNameStrings.fieldNameText]
let queryCompletionBlock = {
(cursor: CKQueryOperation.Cursor?, error: Error?) in
if let error = error {
print(error.localizedDescription)
} else if let cursor = cursor {
let queryOperation = CKQueryOperation(cursor: cursor)
queryOperation.desiredKeys = [DatabaseNameStrings.fieldNameCreatedAt, DatabaseNameStrings.fieldNameText]
queryOperation.queryCompletionBlock = queryCompletionBlock // Error message "Variable used within its own initial value"
queryOperationQueue.addOperation(queryOperation)
}
}
queryOperation.queryCompletionBlock = queryCompletionBlock
queryOperationQueue.addOperation(queryOperation)
一个可行的选项是引用原始查询操作的 queryCompletionBlock
。
let predicate = NSPredicate(value: true)
let query = CKQuery(recordType: DatabaseNameStrings.recordTypeEntry, predicate: predicate)
let queryOperation = CKQueryOperation(query: query)
queryOperation.desiredKeys = [DatabaseNameStrings.fieldNameCreatedAt, DatabaseNameStrings.fieldNameText]
let queryCompletionBlock = {
(cursor: CKQueryOperation.Cursor?, error: Error?) in
if let error = error {
print(error.localizedDescription)
} else if let cursor = cursor {
let newOperation = CKQueryOperation(cursor: cursor)
newOperation.desiredKeys = queryOperation.desiredKeys
newOperation.queryCompletionBlock = queryOperation.queryCompletionBlock
newOperationQueue.addOperation(newOperation)
}
}
queryOperation.queryCompletionBlock = queryCompletionBlock
queryOperationQueue.addOperation(queryOperation)
我如何声明一个从 CloudKit 中检索记录的闭包,该闭包会迭代调用自身直到获取所有记录?下面的代码就是这样做的,除了我收到一条错误消息
Variable used within its own initial value
在我将闭包 queryCompletionBlock 分配给 属性 的行中,在闭包本身的声明中使用与查询操作相同的名称。基本上,我得到一个错误,因为我在声明闭包之前在闭包声明中调用了闭包本身。
let predicate = NSPredicate(value: true)
let query = CKQuery(recordType: DatabaseNameStrings.recordTypeEntry, predicate: predicate)
let queryOperation = CKQueryOperation(query: query)
queryOperation.desiredKeys = [DatabaseNameStrings.fieldNameCreatedAt, DatabaseNameStrings.fieldNameText]
let queryCompletionBlock = {
(cursor: CKQueryOperation.Cursor?, error: Error?) in
if let error = error {
print(error.localizedDescription)
} else if let cursor = cursor {
let queryOperation = CKQueryOperation(cursor: cursor)
queryOperation.desiredKeys = [DatabaseNameStrings.fieldNameCreatedAt, DatabaseNameStrings.fieldNameText]
queryOperation.queryCompletionBlock = queryCompletionBlock // Error message "Variable used within its own initial value"
queryOperationQueue.addOperation(queryOperation)
}
}
queryOperation.queryCompletionBlock = queryCompletionBlock
queryOperationQueue.addOperation(queryOperation)
一个可行的选项是引用原始查询操作的 queryCompletionBlock
。
let predicate = NSPredicate(value: true)
let query = CKQuery(recordType: DatabaseNameStrings.recordTypeEntry, predicate: predicate)
let queryOperation = CKQueryOperation(query: query)
queryOperation.desiredKeys = [DatabaseNameStrings.fieldNameCreatedAt, DatabaseNameStrings.fieldNameText]
let queryCompletionBlock = {
(cursor: CKQueryOperation.Cursor?, error: Error?) in
if let error = error {
print(error.localizedDescription)
} else if let cursor = cursor {
let newOperation = CKQueryOperation(cursor: cursor)
newOperation.desiredKeys = queryOperation.desiredKeys
newOperation.queryCompletionBlock = queryOperation.queryCompletionBlock
newOperationQueue.addOperation(newOperation)
}
}
queryOperation.queryCompletionBlock = queryCompletionBlock
queryOperationQueue.addOperation(queryOperation)