How do I perform text search on Cloudkit records?

Refresh

December 2018

Views

697 time

2

I have Cloudkit records with a 'Keywords' field of type 'text'

I can use this pattern to fetch just the Keywords field of ALL records

let predicate = NSPredicate(value: true)
let query = CKQuery(recordType: "Library", predicate: predicate)
var operation = CKQueryOperation(query: query)  
operation.desiredKeys = ["KeyWords"]
operation.recordFetchedBlock = { [weak self] (record:CKRecord!) in
  ...
}
operation.queryCompletionBlock = { [weak self] (cursor:CKQueryCursor!, error:NSError!) in
  ...
}
self.publicDatabase!.addOperation(operation)

I'd like to limit the returned records to those where the KeyWords field matches a search string which I assume means providing a suitable predicate.

The documentation indicates that CONTAINS is only for lists UNLESS you use the self keyword which searches all text fields

However, replacing the predicate declaration with:

let predicate = NSPredicate(format: "SELF CONTAINS[c] %@", searchString)

results in

*** Terminating app due to uncaught exception 'CKException', reason: 'Predicate comparison options are not supported for expression: SELF CONTAINS[c]

and using

let predicate = NSPredicate(format: "SELF CONTAINS %@", searchString)

results in

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

I am sure it is something straightforward but the docs are not complete yet

1 answers

2

Вы можете использовать этот предикат для маркеров поиска:

NSPredicate(format: "allTokens TOKENMATCHES[cdl] %@", tokens)