15
votes

**I'm a bit at a loss about how to format an NSPredicate where "any" of a certain relationship matches more than one condition.

So for example, say I'm doing a fetch request for an "Employer" entity and I want to get all employers that have at least one employee under age 18. So, I can use "ANY employees.age < 18" - simple enough. But say I want to match any that have an employee under 18 who also has the last name "Howser." That is, it's not enough for the employer to match both "any employee under 18" and "any employee named 'Howser'" - they have to have at least one employee that matches both conditions.

How would this predicate be formatted?

1

1 Answers

16
votes

This is one of the rare circumstances where you need SUBQUERY:

SUBQUERY(employees, $e, $e.age < 18 && $e.lastName == 'Howser').@count > 0