How can I use NSPredicate to filter on core data relationships?

心已入冬 提交于 2019-11-30 05:26:43

Since you have a to-many relationship with the sub object, the subs property of obj returns a set instead of a single object. To query the set, you need to use a SUBQUERY.

Subqueries have the form:

SUBQUERY(collection, $individualCollectionItem, expression-with-collection-item)

in this case you would want something like

SUBQUERY(subs,$s,$s.propertyB==%@) AND SUBQUERY(subs,$s,$s.propertyC!=NULL)

The solution seems to be:

[NSPredicate predicateWithFormat:@"propertyA == %@ AND (SUBQUERY(sub, $s, $s.propertyB == %@ AND $s.propertyC == %@).@count != 0)",  propertyAvalue, propertyBvalue, propertyCvalue];

where the values at the end are the values you want the various properties to be equal to.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!