如何从cloudkit下载多个记录

时间:2016-07-31 19:43:26

标签: ios swift xcode cloudkit predicate

我正在尝试从云端套件下载多个项目,但我收到错误“无法分配类型值(CKQueryCursor!,NSError) - >()键入(CKQueryCursor?,NSError?) - > void”

let locationToLookFor = CLLocation() 
let predicate = NSPredicate(format: "location = %@", locationToLookFor as CLLocation)
let query = CKQuery(recordType: "Location", predicate: predicate)
let operation = CKQueryOperation(query: query)
operation.recordFetchedBlock = self.recordFetchBlock
operation.queryCompletionBlock =
{
    [weak self]
    (cursor: CKQueryCursor!, error: NSError) in
    if(cursor != nil)
    {
        print("Fetching records")
        let newOperation = CKQueryOperation(cursor: cursor)
        operation.recordFetchedBlock = recordFetchBlock
        operation.queryCompletionBlock = operation.queryCompletionBlock
        self!.operationQueue.addOperation(newOperation)
    }
    else {
        print("We have fetched all data")
    }
}
operationQueue.addOperation(operation)

1 个答案:

答案 0 :(得分:2)

您的封闭签名与所需签名不符。如错误消息所示,cursor应该是可选的error。您还会收到错误,因为在将其提供给新操作时,您不会解包cursor

尝试:

operation.queryCompletionBlock =
{
    [weak self]
    (cursor: CKQueryCursor?, error: NSError?) -> Void in
    if let cursor = cursor
    {
        print("Fetching records")
        let newOperation = CKQueryOperation(cursor: cursor)
        operation.recordFetchedBlock = recordFetchBlock
        operation.queryCompletionBlock = operation.queryCompletionBlock
        self?.operationQueue.addOperation(newOperation)
    }
    else {
        print("We have fetched all data")
    }
}