CKFetchRecordsOperation

CKFetchRecordsOperation

在大量搜索示例代码后设法将CKFetchRecordsOperation拼凑在一起;这是...但是我一定错过了一些东西。不要误会我的意思,它可以治疗...但是...

要执行CKFetchRecordsOperation,您需要一个NSArray的CKRecordIDs。要获取CKRecordIDs的NSArray,您需要执行CKQuery,从而可以构建CKRecordIDs的NSArray。

但是请稍等,提取CKRecordID的过程使用了CKQuery,无论如何我都可以简单地下载CKRecords?

如果不使用CKQuery,如何获取CKRecordID的NSArray?

-(void)doSingleBeaconsDownload
{
    CKDatabase *publicDatabase = [[CKContainer containerWithIdentifier:@"iCloud.cqd.ch.BeaconBrowser"] publicCloudDatabase];
    NSPredicate *predicatex = [NSPredicate predicateWithFormat:@"iBeaconConfig = %@",iBeaconsConfirmed.giReferenceID];
    CKQuery *query = [[CKQuery alloc] initWithRecordType:@"Running" predicate:predicatex];

    [self.delegate performSelectorOnMainThread:@selector(processCompleted:) withObject:@"Downloading Configuration ..." waitUntilDone:YES];

    [publicDatabase performQuery:query inZoneWithID:nil completionHandler:^(NSArray *results, NSError *error) {

        if (error) {
            NSLog(@"Batch Download Error iCloud error %@",error);
        }
        else {
            NSMutableArray *rex2download = [[NSMutableArray alloc] init];
            for (CKRecord *rex in results) {
                [rex2download addObject:rex.recordID];
            }

            CKFetchRecordsOperation *fetchRecordsOperation = [[CKFetchRecordsOperation alloc] initWithRecordIDs:rex2download];

           /* fetchRecordsOperation.perRecordCompletionBlock = ^(CKRecord *record, CKRecordID *recordID, NSError *error) {
                if (error) {
                    // Retain the record IDs for failed fetches
                }
                else {
                   // Do something with each record downloaded
                }
            };*/

            fetchRecordsOperation.perRecordProgressBlock = ^(CKRecordID *record, double recordsDownloaded) {
                if (error) {
                    // damn ...
                } else {
                    NSLog(@"Downloaded %f", recordsDownloaded);
                }
            };
            fetchRecordsOperation.fetchRecordsCompletionBlock = ^(NSDictionary *recordsByRecordID, NSError *error) {
                if (error) {
                    // Failed to fetch all or some of the records
                }
                else {
                    for(CKRecord *record in results) {
                        NSLog(@"Fini download %lu",(unsigned long)[recordsByRecordID count]);
                    }
                    [self.delegate performSelectorOnMainThread:@selector(beaconsDownloaded:) withObject:noOf waitUntilDone:YES];
                }
            };
            [publicDatabase addOperation:fetchRecordsOperation];
        }
    }];
}

最佳答案

从Apple Documentation:CKFetchRecordsOperation对象从iCloud检索CKRecord对象(您已经知道其ID)。

CKQueryOperation用于基于某些查询从iCloud检索CKRecord,因此即使您不知道它们的recordID,也可以获取它们。仅当您具有CKRecordID时,才使用CKFetchRecordsOperation。您可以创建CKRecordID而不访问iCloud,并且可以将它们存储在您拥有的任何本地存储中。

我用于这种操作的一个很好的用例是,当您想修改CKRecord时,需要首先从iCloud中检索它(使用CKFetchRecordsOperation),然后再使用CKModifyRecordsOperation将其保存回来。

看看CloudKit上的两个WWDC 2014视频,它们很好地解释了这一点。

10-08 12:23