Feta
Feta

Reputation: 532

How to filter Core Data by entities not in relationship

I have an NSManagedObject SchoolClass that has a To Many relationship to Students. I have it working for when I want to find SchoolClasses containing a specified student by using,

NSPredicate* predicate = [NSPredicate predicateWithFormat:@"(ANY students == %@)", student];

How would I set up an NSPredicate to return an NSArray of SchoolClasses that do NOT contain a specified student? I thought something like below would work but it does not.

NSPredicate* predicate = [NSPredicate predicateWithFormat:@"(NONE students IN %@)", student];

Upvotes: 0

Views: 86

Answers (1)

Patrick Goley
Patrick Goley

Reputation: 5417

IN is used when the right-hand side is a collection, such as an array of students. Try this predicate instead:

[NSPredicate predicateWithFormat:@"(NONE students = %@)", student];

This is also equivalent to ALL students != %@.

This will only return SchoolClasses who do not include the specified student.

Upvotes: 1

Related Questions