【问题标题】:Swift: long query in Parse (OR/AND condition)Swift:Parse 中的长查询(OR/AND 条件)
【发布时间】:2015-06-19 08:43:35
【问题描述】:

我需要使用 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,然后合并结果。有没有办法解决?

【问题讨论】:

  • 在谓词中使用“objectId == %@”。
  • 没有 Keybur,= %@ 没问题

标签: ios objective-c iphone swift parse-platform


【解决方案1】:

我自己是这样解决的,希望对大家有帮助:

let query = PFQuery(className: "MyClassName")
query.whereKey("objectId", containedIn: conditions)
query.findObjectsInBackgroundWithBlock { (objs:[AnyObject]?,error: NSError?) -> Void in
    NSLog("\(objs)")
}

【讨论】:

  • 这非常有效,当在包含 110 万条记录的表中搜索 1.5k 条件时,经过测试,它在 2 秒内返回 1k 结果。
猜你喜欢
  • 2013-01-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-10-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-03-25
相关资源
最近更新 更多