简体   繁体   中英

Can't fetch data from iCloud using CloudKit's CKQueryOperation

I have implemented a CloudKit fetching function. 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. I wonder where the problem is.

update on 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 . 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()

    }
  }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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