38
votes

In an almost identical situation to this question, only I'm looking for all records of one type that are not in any to-many relationship with another type.

So let's say I've got a set of patients, and a set of lists. Patients can belong to multiple lists, and a list can contain multiple patients.

How do I find all of the patients that aren't on any list? I'm using a Core Data model.

UPDATE: Figured it out, but since I have <100 reputation, I can't answer my own question. Here's what I did:

NSPredicate *predicate = [NSPredicate
                          predicateWithFormat:@"lists.@count == 0"];
[fetchRequest setPredicate:predicate];

Then when I ran the fetch request, it only brought up the patients with no list attached.

2

2 Answers

55
votes

Here's what you should do:

NSPredicate *predicate = [NSPredicate
                          predicateWithFormat:@"lists.@count == 0"];
[fetchRequest setPredicate:predicate];

;) wonder where I came up with that solution...

9
votes

Figured it out. Here's what I did:

NSPredicate *predicate = [NSPredicate
                          predicateWithFormat:@"lists.@count == 0"];
[fetchRequest setPredicate:predicate];

Then when I ran the fetch request, it only brought up the patients with no list attached.