Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CloudKit Unexpected Expression for NSPredicate

Tags:

ios

cloudkit

I'm using CloudKit and wish to perform a search for records based on their string fields.

Apple docs say this is the way to do a tokenized search of a record's fields:

To perform a tokenized search of a record’s fields, use the special operator self. A tokenized search searches any fields that have full-text search enabled, which is all string-based fields by default. Listing 5 shows an example that searches the fields of the record for the token strings bob and smith. Each distinct word is treated as a separate token for the purpose of searching. Comparisons are case- and diacritic-insensitive. These token strings may be found in a single field or in multiple fields but all of the tokens must be present in a record for it to be considered a match.

Listing 5: Matching a field containing a tokenized string

NSPredicate predicate = nil;
predicate = [NSPredicate predicateWithFormat:@"self contains 'bob smith'"];

When I enter this exact string for the predicate, I get the an exception.

Code:

predicate = [NSPredicate predicateWithFormat:@"self contains 'bob smith'"];
query = [[CKQuery alloc] initWithRecordType:kCKRecord_Level predicate:predicate];

Exception:

*** Terminating app due to uncaught exception 'CKException', reason: 'Unexpected expression: SELF CONTAINS "bob smith"'

Any ideas what could be wrong? Has anyone had any success with that predicate string and CloudKit?

https://developer.apple.com/library/prerelease/ios/documentation/CloudKit/Reference/CKQuery_class/index.html

like image 759
deepkanwal Avatar asked Apr 20 '26 13:04

deepkanwal


1 Answers

It looks like the 'self contains' does not work anymore. You still do a tokenized search using this predicate:

NSPredicate(format: "allTokens TOKENMATCHES[cdl] %@", "bob smith")
like image 144
Edwin Vermeer Avatar answered Apr 22 '26 07:04

Edwin Vermeer