Search code examples
swiftnspredicatecloudkitckquery

How can I make a CKQuery without predicate?


I need to query all the record types, without any filtering. I tried two ways:

let query = CKQuery(recordType: "Pm", predicate: nil)
let query = CKQuery(recordType: "Pm", predicate: NSPredicate(format: ""))

I got error:

'NSInvalidArgumentException', reason: 'Unable to parse the format string ""'

How to do?


Solution

  • You can't perform a query without a predicate, but NSPredicate does have a method just for this, and in Swift it's used through the initializer:

    init(value: Bool) -> NSPredicate // return predicates that always evaluate to true/false
    

    Usage is as follows.

    let predicate = NSPredicate(value: true)