Search code examples
iosswiftnspredicatecloudkit

CloudKit - NSPredicate for finding all records that contains multiple CKReferences in a reference list


I'm working on Joke app that uses CloudKit

Each joke has a reference list to some categories/tags.

I'm trying to query all jokes that has some specific tags. For instance I want to find all jokes that is in the categories of Animal and Doctor.

Right now I have tried with the following code

let tagRecords = tags.map {  CKReference(record: $0.record, action: .None) }
let predicate = NSPredicate(format: "tags CONTAINS %@", tagRecords)
let query = CKQuery(recordType: "Jokes", predicate: predicate)

Basically what the above does is first of all creating an array of references and then make a predicate to find the tags the contains those references

Unfortunately this doesn't work I get the following error

server message = "Internal server error""

So the question is: How do you find all records that contains all references in a reference list?


Solution

  • Assuming that tags is a relationship or an array of strings, you could try with the following predicate:

    let predicate = NSPredicate(format: "ANY tags IN %@", tagRecords)
    

    Hope this helps.