Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Search Core Data for all objects with an empty "to-many" relationship

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.

like image 503
bryanjclark Avatar asked Feb 08 '12 05:02

bryanjclark


2 Answers

Here's what you should do:

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

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

like image 132
Michael Frederick Avatar answered Nov 15 '22 14:11

Michael Frederick


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.

like image 9
bryanjclark Avatar answered Nov 15 '22 15:11

bryanjclark