NSCompoundPredicate and Cloudkit

1.4k Views Asked by At

I'm trying to do this with a compound predicate using Cloudkit, but Xcode error says "Unexpected Expression". Anyone know what is wrong with my code? Appreciate any help!

let userRef = CKReference(recordID: userID, action: .None)

    let predicateOne = NSPredicate(format: "recordID IN %@ AND user = %@", postIDs, userRef)
// I need all user post's that match the IDs in the array and where the user ID matches the Id in the reference field for the user.

    let predicateTwo = NSPredicate(format: "recordID IN %@", followIDs)
// Or I want recordID's that match the IDs in this array.
// I want records if either predicate condition is met, or both, which is why I'm using an OrPredicateType.

    let predicate = NSCompoundPredicate(type: NSCompoundPredicateType.OrPredicateType, subpredicates: [predicateOne!, predicateTwo!])
    let query = CKQuery(recordType: "UserPosts", predicate: predicate)
    let queryOp = CKQueryOperation(query: query)
1

There are 1 best solutions below

6
On

There are some rules for using A NSPredicate for CloudKit. For more information see the Apple documentation

What you could try is create the predicate like this:

NSPredicate(format: "recordID IN %@ OR (recordID IN %@ AND user = %@)", followIDs, postIDs, userRef)

But I have bad experience with using predicates with both AND and OR statements in it. It this predicate does not work, then I think the only solution is executing 2 separate queries and then combining the result. You could do that by using the union function from the ExSwift library