NSCompoundPredicate和Cloudkit

时间:2015-03-31 18:54:55

标签: swift nspredicate cloudkit nscompoundpredicate

我正在尝试使用Cloudkit对复合谓词执行此操作,但Xcode错误表示“意外表达”。有人知道我的代码有什么问题吗?感谢任何帮助!

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 个答案:

答案 0 :(得分:2)

使用A NSPredicate for CloudKit有一些规则。有关详细信息,请参阅Apple documentation

您可以尝试创建这样的谓词:

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

但是我在使用包含AND和OR语句的谓词方面经验不佳。这个谓词不起作用,那么我认为唯一的解决方案是执行2个单独的查询然后组合结果。您可以使用ExSwift库中的union函数

来实现