CloudKit: Query the index ( recordID.recordName )

1.9k views Asked by At

I'm trying to figure out how to query the index of my records in cloudkit using Objective-C. I build a query to fetch all my records:

CKContainer *container = [CKContainer containerWithIdentifier:containerID];
CKDatabase *publicDatabase = [container publicCloudDatabase];
CKQuery *query = [[CKQuery alloc] initWithRecordType:recordType
                                           predicate:[NSPredicate predicateWithFormat:@"TRUEPREDICATE"]];
[publicDatabase performQuery:query
                inZoneWithID:nil
           completionHandler:^(NSArray *results, NSError *error)
 {

     if (!error)
     {
     NSLog(@"results query %@", results);

     }

     else
     {
         NSLog(@"FETCH ERROR: %@", error);
     }
 }];

But I need to get the index of each record. Any of you knows how can only query the index of all records?

I'll really appreciate your help

2

There are 2 answers

2
Edwin Vermeer On

instead of a CKQuery use a CKQueryOperation. you could then set the desiredKeys property to limit the result to only the id's. You could use something like:

    CKContainer *container = [CKContainer containerWithIdentifier:containerID];
    CKDatabase *publicDatabase = [container publicCloudDatabase];
    CKQuery *query = [[CKQuery alloc] initWithRecordType:recordType
                                               predicate:[NSPredicate predicateWithFormat:@"TRUEPREDICATE"]];
    CKQueryOperation *queryOp = [[CKQueryOperation alloc] initWithQuery:query];
    queryOp.desiredKeys = @[@"record.recordID.recordName"];
    queryOp.recordFetchedBlock = ^(CKRecord *record)
    {
        // do something...
    };

     queryOp.queryCompletionBlock = ^(CKQueryCursor *cursor, NSError *error)
    {
        // do something else...
    };

    queryOp.resultsLimit = CKQueryOperationMaximumResults;
    [publicDatabase addOperation:queryOp];
0
user2924482 On

This the Objective-C answer:

 CKContainer *container = [CKContainer containerWithIdentifier:containerID];
    CKDatabase *publicDatabase = [container publicCloudDatabase];
    CKQuery *query = [[CKQuery alloc] initWithRecordType:recordType
                                               predicate:[NSPredicate predicateWithFormat:@"TRUEPREDICATE"]];
     CKQueryOperation *queryOp = [[CKQueryOperation alloc] initWithQuery:query];
    queryOp.desiredKeys = @[@"record.recordID.recordName"];
    queryOp.recordFetchedBlock = ^(CKRecord *record)
    {
        // do something...
    };

     queryOp.queryCompletionBlock = ^(CKQueryCursor *cursor, NSError *error)
    {
        // do something else...
    };

    queryOp.resultsLimit = CKQueryOperationMaximumResults;
    [publicDatabase addOperation:queryOp];