Amiram Stark
Amiram Stark

Reputation: 2208

Core Data - Fetch all objects NOT in a relationship

I have a core data entity A that has a one-to-many relationship with entity B. Given a set of instances of entity B, how do I retrieve all instances of A that are NOT in a relationship with those instances of B? (I'm talking about IOS core data, if that matters).

Upvotes: 1

Views: 770

Answers (1)

omz
omz

Reputation: 53551

NSSet *bEntities = a.b;
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF NOT IN %@", bEntities];
NSManagedObjectContext *moc = ...;
NSFetchRequest *fetchRequest = [[[NSFetchRequest alloc] init] autorelease];
[fetchRequest setEntity:[NSEntityDescription entityForName:@"B" inManagedObjectContext:moc]];
NSArray *result = [moc executeFetchRequest:fetchRequest];

Upvotes: 2

Related Questions