我将如何声明一个从 CloudKit 检索记录的闭包,该闭包迭代地调用自身,直到获取所有记录?下面的代码可以做到这一点,除了我得到一个错误说
在自己的初始值内使用的变量
在我将闭包 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)