我已经实现了CloudKit抓取功能。 不过我这里有些问题。当我使用下面的代码时
-(void) centralManager:(CBCentralManager *)central didDiscoverPeripheral:(CBPeripheral *)peripheral advertisementData:(NSDictionary *)advertisementData RSSI:(NSNumber *)RSSI {
NSLog(@"Discovered peripheral %@ (%@)",peripheral.name,peripheral.identifier.UUIDString);
if (![self.discoveredPeripherals containsObject:peripheral] ) {
dispatch_async(dispatch_get_main_queue(), ^{
[self.discoveredPeripherals addObject:peripheral];
[self.tableview insertRowsAtIndexPaths:@[[NSIndexPath indexPathForRow:self.discoveredPeripherals.count-1 inSection:0]] withRowAnimation:UITableViewRowAnimationLeft];
});
}
[central connectPeripheral:peripheral options:nil];
}
-(void) centralManager:(CBCentralManager *)central didConnectPeripheral:(CBPeripheral *)peripheral {
peripheral.delegate = self;
if(peripheral.services)
[self peripheral:peripheral didDiscoverServices:nil]; //already discovered services, DO NOT re-discover. Just pass along the peripheral.
else
[peripheral discoverServices:nil];
}
-(void) peripheral:(CBPeripheral *)peripheral didDiscoverServices:(NSError *)error {
for(CBService* svc in peripheral.services)
{ NSLog(@" service %@",svc.description);
if(svc.characteristics)
[self peripheral:peripheral didDiscoverCharacteristicsForService:svc error:nil];
else
[peripheral discoverCharacteristics:nil forService:svc];
}
}
-(void) peripheral:(CBPeripheral *)peripheral didDiscoverCharacteristicsForService:(CBService *)service error:(NSError *)error {
for (CBCharacteristic *characteristic in service.characteristics ) {
NSLog(@"Discovered characteristic %@(%@)",characteristic.description,characteristic.UUID.UUIDString);
NSLog(@"Characteris is %@",[characteristic description]);
}
}
我可以成功获取数据。但是,一旦我改变代码,如
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)
}
})
使用CKQueryOperation,我将无法获取数据。我想知道问题出在哪里。
更新10.8
我一直在努力解决这个问题。我发现该应用甚至没有输入 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)
和recordFetchedBlock
。我不知道为什么会这样。
答案 0 :(得分:0)
最后,今天我修复了您请求的代码的错误。它现在正在工作......请问我是否需要进一步的帮助。感谢。
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()
}
}
}