1
votes

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
Why do you want to compare with a record ID? - Amin Negm-Awad
@AminNegm-Awad Because I need all the records with ID greater then 23 or any giving number - user2924482
Okay, why do you need records with an ID greater than a given number? An ID is an Id, no property. - Amin Negm-Awad
@AminNegm-Awad that is exactly what I tried to do but did work. "recordID.recordName" - user2924482
I did not say, that you should do something. So you cannot do, what I intended to do. You went to SO to get a solution for your problem. It would make things easier, if you respond to the questions. Again: Why do you need records with an ID greater than a given number? - Amin Negm-Awad

2 Answers

1
votes

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
votes

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.