问题
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).
回答1:
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];
来源:https://stackoverflow.com/questions/6801781/core-data-fetch-all-objects-not-in-a-relationship