CloudKit : CKQuery showing error: error: The operation couldn’t be completed. (CKErrorDomain error 1.)

1k Views Asked by At

I working on iOS app implementing cloudkit but I need to query all the records with ID greater then a number. For example I have record with ID of 23:

enter image description here

here is my code:

CKContainer *myContainer = [CKContainer containerWithIdentifier:containerID];

    CKDatabase *publicDatabase = [myContainer publicCloudDatabase];


CKRecordID *recordID = [[CKRecordID alloc] initWithRecordName:@"23"];
    CKReference* recordToMatch = [[CKReference alloc] initWithRecordID:recordID action:CKReferenceActionNone];
    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"recordID >= %@", recordToMatch];
    CKQuery *query = [[CKQuery alloc] initWithRecordType:recordType predicate:predicate];

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

            NSLog(@"error: %@", error.localizedDescription);
        }
        else {

        }
    }];

But I'm getting the following error:

error: error: The operation couldn’t be completed. (CKErrorDomain error 1.)

Any of you knows how can I setup my NSPredicate in a way where I can get all the records with ID greater then 23 ?

I'll really appreciate your help.

2

There are 2 best solutions below

6
On

For a query like this you could use a predicate like:

in Swift:

var predicate = NSPredicate(format: "recordID >= %@", CKRecordID(recordName: "23"))

In Objective C:

 NSPredicate *predicate = [NSPredicate predicateWithFormat:@"recordID >= %@", [CKRecordID initWithRecordName:@"23"]];

Then I do assume that you created the CKRecords object originally while specifying this number. Otherwise recordID values will be assigned by CloudKit and will be a GUID.

0
On

Your recordID of 23 is stored as a CKRecordID type and not as a number thus you cannot use any numeric predicates like greater-than. You should create a new number field in your record for storing your integer IDs and query that instead.