Say I have core data objects of type "obj" that has a property "propertyA" and a one-to-many relationship with an object of type "sub" that has two properties, "propertyB" and "propertyC".
I want to fetch all the objs that have propertyA equal to a value and a sub obj with propertyB and propertyC set.
If it was just propertyA and propertyB, I would do
[NSPredicate predicateWithFormat:@"ANY sub.propertyB = %@ AND propertyA == %@", ...];
The problem is that I can't figure out how to add in the second property. I want only the objs that have at least one sub that has the two properties true. I've tried the following, but it doesn't work:
[NSPredicate predicateWithFormat:@"ANY (sub.propertyB = %@ AND sub.propertyC) AND propertyA == %@", ...];
I've tried it without the ANY but that doesn't work either. How can I do this?
The solution seems to be:
where the values at the end are the values you want the various properties to be equal to.
Since you have a to-many relationship with the sub object, the
subs
property ofobj
returns a set instead of a single object. To query the set, you need to use a SUBQUERY.Subqueries have the form:
in this case you would want something like