Search code examples
iossqlnspredicatecloudkit

NSPredicate and BEGINSWITH with CloudKit : Field value type mismatch


I have a table "Store" with the attributes "name" of type String (indexes checked for sort query and search). I want to execute a SQL like query to find all stores with name beginning with a substring.

So I tried this predicate :

NSPredicate *pred = [NSPredicate predicateWithFormat:@"ANY name BEGINSWITH %@",substring];

CKQuery *query = [[CKQuery alloc]initWithRecordType:@"Store" predicate:pred];

CKQueryOperation *queryOperation = [[CKQueryOperation alloc] initWithQuery:query];

queryOperation.desiredKeys = @[@"name",@"category"];

NSMutableArray *results = [[NSMutableArray alloc] init];

queryOperation.recordFetchedBlock = ^(CKRecord *record) {
    [results addObject:record];
};
queryOperation.queryCompletionBlock = ^(CKQueryCursor *cursor, NSError *error) {
   //my own code
}
[publicDatabase addOperation:queryOperation];

I have this error :

<CKError 0x1887a540: "Invalid Arguments" (12/1009); "Field value type mismatch in query predicate for field 'name'"> for query : <CKQuery: 0x18882650; recordType=Store, predicate=ANY name BEGINSWITH "mysubstring">

Solution

  • My guess is that you need to get rid of the "ANY".

    The ANY and SOME aggregate operators may be combined with the IN and CONTAINS operators to perform list membership tests.

    So ANY is for lists (Arrays). Since 'name' is a String, it is not a list, hence the mismatch error: "Field value type mismatch in query predicate for field 'name'"