QueryOp with recordID

121 views Asked by At

I had a recordID.recordName (singleID in this example) and that I wanted to query with queryOp; after some searching and experimentation I managed to put this code together... which works, but is it really correct, is there a shorter path perhaps.

CKRecordID *wellKnownIDx = [[CKRecordID alloc] initWithRecordName:singleID];
CKReference *singleREX = [[CKReference alloc] initWithRecordID:wellKnownIDx action:CKReferenceActionDeleteSelf];

CKDatabase *publicDatabase = [[CKContainer containerWithIdentifier:@"iCloud.blah"] publicCloudDatabase];
NSPredicate *predicatex = [NSPredicate predicateWithFormat:@"recordID == %@",singleREX];

CKQuery *query = [[CKQuery alloc] initWithRecordType:@"Courses" predicate:predicatex];
CKQueryOperation *queryOp =[[CKQueryOperation alloc] initWithQuery:query];
queryOp.desiredKeys = @[@"record.recordID.recordName"];
queryOp.resultsLimit = 1;

queryOp.recordFetchedBlock = ^(CKRecord *results)
{
    NSLog(@"Student Found %@",results.recordID.recordName);
};

queryOp.queryCompletionBlock = ^(CKQueryCursor *cursor, NSError *error)
{
    NSLog(@"CKQueryCursor  error %@", error);
};

[publicDatabase addOperation:queryOp];
1

There are 1 answers

0
karbonator On

What you have should work, but it looks like all you want to do is to fetch a record with a known recordID. There is no need to use a query for that. It's more code than necessary and will also require a query index on the field.

Instead, use the fetchRecordWithID method on CKDatabase: https://developer.apple.com/library/ios/documentation/CloudKit/Reference/CKDatabase_class/index.html#//apple_ref/occ/instm/CKDatabase/fetchRecordWithID:completionHandler: