1

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.

user2924482
  • 8,380
  • 23
  • 89
  • 173
  • Why do you want to compare with a record ID? – Amin Negm-Awad May 31 '15 at 04:32
  • @AminNegm-Awad Because I need all the records with ID greater then 23 or any giving number – user2924482 May 31 '15 at 04:41
  • Okay, why do you need records with an ID greater than a given number? An ID is an Id, no property. – Amin Negm-Awad May 31 '15 at 04:52
  • @AminNegm-Awad that is exactly what I tried to do but did work. "recordID.recordName" – user2924482 May 31 '15 at 05:08
  • 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 May 31 '15 at 05:19
  • Because I have records with ID greater than a given number. In this case I'm looking for records with ID of 24,25,26 ... n . – user2924482 May 31 '15 at 06:21

2 Answers2

1

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.

Edwin Vermeer
  • 13,017
  • 2
  • 34
  • 58
  • I tried you solution but I get this error: Field '___recordID' has a value type of REFERENCE and cannot be queried using filter type GREATER_THAN_OR_EQUALS – user2924482 May 31 '15 at 17:31
  • Then the only way to achieve this is by adding an extra text field with the same value and then just query that text field. – Edwin Vermeer May 31 '15 at 18:47
  • Can you explain with a example? – user2924482 May 31 '15 at 19:22
  • When creating a record also creat and fill a field like for instance myID, Then the predicate will just be: NSPredicate *predicate = [NSPredicate predicateWithFormat:@"myID >= %@",@"23"]; – Edwin Vermeer Jun 01 '15 at 06:40
  • I have update my original post as you mention I have change my predicate but I get the error: The operation couldn’t be completed. (CKErrorDomain error 1.) – user2924482 Jun 01 '15 at 07:12
  • I don't see a change in your original post. You still have a predicate for recordID – Edwin Vermeer Jun 01 '15 at 13:50
0

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.

malhal
  • 26,330
  • 7
  • 115
  • 133