NSPredicate 检索具有特定孩子的所有父母? (IOS)
假设我在 IOS 中有以下核心数据结构:
Parent1
Child1.1: Text='A', Number=1
Child1.2: Text='B', Number=1
Parent2
Child2.1: Text='B', Number=1
Child2.2: Text='A', Number=2
Parent3
Child3.1: Text='B', Number=2
Child3.2: Text='A', Number=1
我想检索所有拥有 Text='A' 和 Number=1 的孩子的父母(上例中的 Parent1 和 Parent3)。
基本上我需要的是父实体的谓词,如下所示:
ANY (child.Text = 'A' AND child.Number = 1)
但这不起作用 - 似乎我不能在 ANY 关键字后面添加括号。
以下也不好(即使它是有效的):
ANY child.Text = 'A' AND ANY child.Number = 1
因为它也返回 Parent2 。
有没有办法在谓词中执行此操作,或者我是否必须以编程方式执行此操作(例如,仅检索子级而不使用 ANY 关键字并从后向关系构造父级数组)?
Say I have the following Core Data structure in IOS:
Parent1
Child1.1: Text='A', Number=1
Child1.2: Text='B', Number=1
Parent2
Child2.1: Text='B', Number=1
Child2.2: Text='A', Number=2
Parent3
Child3.1: Text='B', Number=2
Child3.2: Text='A', Number=1
I want to retrive all parents that have a child with Text='A' and Number=1 (Parent1 and Parent3 in the above example).
Basically what I need is a predicate on the Parent entity that looks like:
ANY (child.Text = 'A' AND child.Number = 1)
But this doesn't work - it seems that I cannot have parentheses following an ANY keyword.
The following isn't good either (even though it's valid):
ANY child.Text = 'A' AND ANY child.Number = 1
because it returns Parent2 as well.
Is there a way to do this in a predicate, or do I have to do it programmatically (e.g. retrieve just the children without using an ANY keyword and construct an array of parents from the back relationships)?
在谓词中,您必须使用 == 来表示相等 - 那么它有效吗?
您还可以查看 NSCompoundPredicate,用于将谓词部分与 AND 和 OR 组合,例如
In a predicate you have to use == for equality - does it work then?
You can also have a look at the NSCompoundPredicate for combining predicate parts with AND and OR, like