CKFetchRecordsOperation + CKQueryOperations ...我缺少什么? [英] CKFetchRecordsOperation + CKQueryOperations ... what am I missing?

查看:87
本文介绍了CKFetchRecordsOperation + CKQueryOperations ...我缺少什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Managed to cobble together a CKFetchRecordsOperation after much searching for sample code; and here it is... but I must have missed something. Don't get me wrong it works a treat... but...

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

To execute a CKFetchRecordsOperation you need an NSArray of CKRecordIDs; to get a NSArray of CKRecordIDs, you need to execute CKQuery thru which you can build your NSArray of CKRecordIDs.

但是请稍等,提取CKRecordIDs的过程将使用CKQuery,通过它我仍然可以直接下载CKRecords?

But wait a minute, the process of extracting the CKRecordIDs uses a CKQuery, thru which I could simply download the CKRecords anyway?

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

How do you get your NSArray of CKRecordIDs if not with a CKQuery?

-(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 文档 :CKFetchRecordsOperation对象从iCloud检索CKRecord对象(您已经知道其ID)。

From Apple Documentation: A CKFetchRecordsOperation object retrieves CKRecord objects (whose IDs you already know) from iCloud.

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

A CKQueryOperation is used to retrieve CKRecords from iCloud based on some Query, so you can get them even if you do not know their recordIDs. A CKFetchRecordsOperation is used ONLY when you have the CKRecordIDs. You can create a CKRecordID without accessing iCloud, and you can store them in any local storage you have.

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

A good use case, which I use for this kind of operation, is when you want to modify a CKRecord, you need to first retrieve it from iCloud (using CKFetchRecordsOperation) and then save it back using CKModifyRecordsOperation.

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

Have a look at the two WWDC 2014 Videos on CloudKit that explain this pretty well.

这篇关于CKFetchRecordsOperation + CKQueryOperations ...我缺少什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆