NSPredicate 获取匹配两个条件的关系的实体

NSPredicate to get entity of a relationship matching two conditions

我正在使用 Core Data 来获取实体。我只需要获取具有关系 attribute 且 ID 为 41 并且相同 attribute 需要将 isOn 属性 设置为 YES 的实体.

这是我正在尝试的:

[NSPredicate predicateWithFormat:@"(SELF IN %@ AND ANY attributes.attributeID.intValue == 41) AND (SELF IN %@ AND ALL attributes.isOn == %@)", self.places, self.places, @YES];

出于某种原因,这使我的应用程序崩溃,而且没有任何解释。

关于我的谓词格式是否错误有什么想法吗?

编辑:

做更多的研究似乎我需要使用 SUBQUERY 但不太确定在这种情况下如何使用。这是我尝试过的,但它也因 "cannot parse format" 错误而崩溃:

[NSPredicate predicateWithFormat:@"SUBQUERY(SELF IN %@, $e, $e.attributes.attributeID.intValue == %i && $e.attributes.isOn == %@).@count > 0)", self.places, 41, @YES]

处理子查询外的 SELF IN,因为它与 evaluated/fetched 实体相关,而不是相关实体,而且您的 SUBQUERY 格式也有点错误:

[NSPredicate predicateWithFormat:@"SELF IN %@ AND (SUBQUERY(attributes, $e, $e.attributeID.intValue == %i && $e.isOn == %@).@count > 0)", self.places, 41, @YES]

此外,我希望您的关系名称 attributes 仅用于此问题的演示目的。如果没有,我会建议更改它。有一个名为 attributes 的关系真的很令人困惑:它们是相互排斥的概念。