简体   繁体   English

无法使用CloudKit的CKQueryOperation从iCloud中获取数据

[英]Can't fetch data from iCloud using CloudKit's CKQueryOperation

I have implemented a CloudKit fetching function. 我已经实现了CloudKit提取功能。 However I have some problem here. 但是我在这里有一些问题。 When I use the code below 当我使用下面的代码

    let cloudContainer = CKContainer.defaultContainer()
    let publicDatabase = cloudContainer.publicCloudDatabase
    let predicate = NSPredicate(value: true)
    let query = CKQuery(recordType: "Restaurant", predicate: predicate)

    publicDatabase.performQuery(query, inZoneWithID: nil, completionHandler: {
        results, error in
        if error == nil {
            print("Completed the download of Restauran data")
            self.restaurants = results! as [CKRecord]
            dispatch_async(dispatch_get_main_queue(), {
                self.tableView.reloadData()
            })
        } else {
            print(error)
        }
    })

I can successfully fetch the data. 我可以成功获取数据。 However, once I change the code like 但是,一旦我更改了代码

    restaurants = []
    let cloudContainer = CKContainer.defaultContainer()
    let publicDatabase = cloudContainer.publicCloudDatabase

    let predicate = NSPredicate(value: true)
    let query = CKQuery(recordType: "Restaurant", predicate: predicate)

    let queryOperation = CKQueryOperation(query: query)
    queryOperation.desiredKeys = ["name", "image"]
    queryOperation.queuePriority = .VeryHigh
    queryOperation.resultsLimit = 50

    queryOperation.recordFetchedBlock = { (record:CKRecord!) -> Void in
        if let restaurantRecord = record {
            self.restaurants.append(restaurantRecord)
        }
    }

    queryOperation.queryCompletionBlock = { [unowned self] (cursor, error) in
        if (error != nil) {
            print("Failed to get data from iCloud - \(error!.localizedDescription)")
        } else {
            print("Successfully retrieve the data form iCloud")
            dispatch_async(dispatch_get_main_queue(), {
                self.tableView.reloadData()
            })
        }
    }

    publicDatabase.addOperation(queryOperation)

using CKQueryOperation, I will fail to fetch the data. 使用CKQueryOperation,我将无法获取数据。 I wonder where the problem is. 我想知道问题出在哪里。

update on 10.8 在10.8更新

I've been trying lots of way to figure this out. 我一直在尝试很多方法来解决这个问题。 I find that the app don't even enter the recordFetchedBlock and queryCompletionBlock . 我发现该应用程序甚至都没有输入recordFetchedBlockqueryCompletionBlock I don't know why this happens. 我不知道为什么会这样。

Finally, today I fixed bugs for your requested code. 最后,今天我修复了您请求的代码的错误。 It's now working... Please ask me if you need further assistance. 现在正在运作...请问我是否需要进一步的协助。 Thanks. 谢谢。

restaurants = []
let cloudContainer = CKContainer.defaultContainer()
let publicDatabase = cloudContainer.publicCloudDatabase

let predicate = NSPredicate(value: true)
let query = CKQuery(recordType: "Restaurant", predicate: predicate)

let queryOperation = CKQueryOperation(query: query)
queryOperation.desiredKeys = ["name", "image"]
queryOperation.queuePriority = .VeryHigh
queryOperation.resultsLimit = 50

queryOperation.recordFetchedBlock = { (record:CKRecord!) -> Void in

    self.restaurants.append(record)
}

queryOperation.queryCompletionBlock = { (cursor: CKQueryCursor?, error: NSError?) in
  dispatch_async(dispatch_get_main_queue()) {
    if (error != nil) {
        print("Failed to get data from iCloud - \(error!.localizedDescription)")
    } else {
        print("Successfully retrieve the data form iCloud")
        self.tableView.reloadData()

    }
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM