我需要使用Parse SDK执行 compound query 。
我的代码:
func retrieveObjects() {
let conditions: [String] = .....
let query: PFQuery = self.generateCompoundQueryFromConditions(conditions)
query.findObjectsInBackgroundWithBlock { (objs:[AnyObject]?,error: NSError?) -> Void in
NSLog("\(objs)")
}
}
private func generateCompoundQueryFromConditions(conditions: [String]) -> PFQuery {
var queries: [PFQuery] = []
for condition: String in conditions {
var query = PFQuery(className:"MyClassName")
query.whereKey("objectId", equalTo: condition)
queries.append(query)
}
return PFQuery.orQueryWithSubqueries(queries)
}
Conditions.count - > 24个
代码效果很好,但是我收到了这个错误:
[Error]: too many $or clauses (Code: 154, Version: 1.7.4)
有没有办法用很多OR条件进行查询?
我也尝试使用NSPredicate:
private func generateQueryWithPredicateFromConditions(conditions: [String]) -> PFQuery {
var predicates: [NSPredicate] = []
for condition in conditions {
predicates.append(NSPredicate(format: "objectId = %@", condition))
}
let predicate = NSCompoundPredicate.orPredicateWithSubpredicates(predicates)
return PFQuery(className: "MyClassName", predicate: predicate)
}
现在错误是:
'This query is too complex. It had an OR with >4 subpredicates after normalization.'
所以,在N-OR条件下,我将进行N / 4 + N%4 PFQuery然后合并结果。可能是一个解决方案吗?
答案 0 :(得分:1)
我以这种方式解决了我自己,希望可以帮助别人:
let query = PFQuery(className: "MyClassName")
query.whereKey("objectId", containedIn: conditions)
query.findObjectsInBackgroundWithBlock { (objs:[AnyObject]?,error: NSError?) -> Void in
NSLog("\(objs)")
}