0
votes

Suppose two entities: EntityA and EntityB, connected by many to many relationship. Suppose the relation attributes are relatedEntityBs and relatedEntityAs respectively. And I have an NSArray of EntityB instances. I want to find all EntityA instances which have in relation all instances that are in nsarray. That is if the array is [entityB1,.....,entityBn]. Find all EntityA instances, which are related with entityB1,wntityB2 till entityBn. How can I write this kind of predicate?

1
Do you want to check for equality, i.e. find all objects that are related to entityB1, ..., entitiyBn and no others? Then this might be useful: stackoverflow.com/questions/13084930/….Martin R

1 Answers

2
votes

This predicate should do it:

NSMutablArray *predicateArray = [NSMutableArray array];
for (int i=0;i<entityBArray.count;i++){
     NSPredicate *subPredicate = [NSPredicate predicateWithFormat:@"ANY relatedEntityBs contains %@",[entityBArray objectAtIndex:i];
     [predicateArray addObject:subPredicate] ;
} 
NSCompoundPredicate *daddyPredicate = [NSCompoundPredicate orPredicateWithSubpredicates:predicateArray];

Basically each sub predicate checks if relatedEntityB contains a single object of the array and finally the daddyPredicate combines them all in with OR.