EntityA
has a to-many relationship to EntityB
(and EntityB
has a to-one relationship to EntityA
). I have an array of EntityB
s (or more accurately, I have an NSArray
that contains instances of NSManagedObject
which represent EntityB
). I want to create an NSFetchRequest
that will fetch all EntityA
s that have a relationship to at least one of the EntityB
s in the array. How do I write the predicate for this fetch request?
The following works, but I think it is sub-optimal; it's hard to grok and I'm sure there must be a better way of expressing this:
NSArray *entityBs = ...;
NSMutableArray *containsEntityBSubPredicates = [NSMutableArray new];
for (NSManagedObject *entityB in entityBs) {
[containsEntityBSubPredicates addObject:[NSPredicate predicateWithFormat:@"%@ IN entityBs", entityB]];
}
NSPredicate *containsEntityBsPredicate = [NSCompoundPredicate orPredicateWithSubpredicates:containsEntityBSubPredicates];
I've also tried this, but it doesn't work:
NSArray *entityBs = ...;
NSPredicate *containsEntityBsPredicate = [NSPredicate predicateWithFormat:@"ANY %@ in entityBs", entityBs];
Am I missing a simpler solution?