Given the following Core Data Model:
-> : to-one releationship
->>: to-many relationship
Entity A ->> Entity B
Entity B -> A // each B belongs to exactly one A
Entity B ->> B // a B can be related to other B's
So in class A
:
@property (nonatomic, retain) NSSet *Bs;
In class B
:
@property (nonatomic, retain) A *A;
@property (nonatomic, retain) NSSet *Bs;
Assume I have an NSArray *myArray
containing (Bj, Bk)
.
What I want is to fetch all B
's which belong to Ax
and are related to Bj
and Bk
:
The code below works, but is completely ugly, since I have to hardcode the NSPredicate based on the number (here it's 2, but it could be 10 or so) of B
's in the array:
NSEntityDescription *fetchedEntity = [NSEntityDescription entityForName:@"B" inManagedObjectContext:self.managedObjectContext];
[NSPredicate predicateWithFormat:@"A == %@ and (Bs contains %@) and (Bs contains %@)", Ax, [myArray objectAtIndex:0], [myArray objectAtIndex:1]];
How can I rewrite the predicate so that it is more generic? It must be so obvious, but I just don't see it...