针对特定集合的多对多关系核心数据查询

时间:2017-10-29 13:31:40

标签: ios swift core-data

enter image description here

public enum Ability: String {
  case newcomer = "Newcomer"
  case beginner = "Beginner"
  case intermediate = "Intermediate"
  case advanced = "Advanced"
}

public enum Group: String {
  case solo = "Solo"
  case duo = "Duo"
  case team = "Team"
}

我想找到符合以下条件的所有表演。

ability == "Beginner"
group == "Duo"
performers == "Jane Davies" && "Alice Evans"

The documentation表示不支持核心数据中的聚合操作,这是令人惊讶的。这方面的问题一直是asked before,建议使用' Any'。

根据我的理解,'任何'将返回包括' Jane'或者' Alice'或者'简&爱丽丝'对?我只想要包括& Jane& amp;爱丽丝'

如何为此查询编写谓词?

由于

1 个答案:

答案 0 :(得分:1)

使用(不同的) Performer 对象列表,以下谓词应该工作 查找与这些对象完全相关的所有 Performance 对象 表演者:

let perf1 = ... // Performer object for "Jane Davies"
let perf2 = ... // Performer object for "Alice Evans"
let list = [perf1, perf2]

let predicate = NSPredicate(format: "performers.@count = %ld AND ALL performers in %@", list.count, list)

但显然不是。解决方法是使用SUBQUERY:

let predicate = NSPredicate(format: "performers.@count = %ld AND SUBQUERY(performers, $p, $p in %@).@count = %ld",
      list.count, list, list.count)