1

I have 3 entities: Person, Region, and Boundary. Boundary has an attribute of 'state'. A person can have many regions. A region can have many boundaries:

Person <<-->> Region <<-->> Boundary.state

With a Person entity, I would like to find out all the states he has a Boundary in. Can a predicate do this sort of search or will i need to do a nested search:

NSMutableArray *states = [NSMutableArray array];

for (Region *region in person.regions) {
    for (Boundary *boundary in region.boundaries) {
        // add state to array
    }
}

EDIT:

Sorry, I'm not a database person and didn't think it out to realized they are many-to-many relationships:

Person<<-->>Region<<-->>Boundary
4
  • Is there an entity called state? You'll be able to use a predicate to get all the Boundaries that a person has. From there you can work out the states.
    – Fogmeister
    Mar 13, 2013 at 15:36
  • No there isn't a Entity for State, just an attribute in Boundary Entity. Could this be a work around? Create a State entity? It would only contain 1 attribute...
    – Padin215
    Mar 13, 2013 at 15:37
  • No, I'll [put an answer.
    – Fogmeister
    Mar 13, 2013 at 15:37
  • 1
    @Fogmeister: I think that your solution would also have worked. It might be a bit less effective if thePerson.regions is a large set. - Unfortunately the first version of the question was misleading.
    – Martin R
    Mar 13, 2013 at 16:13

2 Answers 2

2

The "ANY" collection operator in predicates does not work with nested to-many relationships. You can use a SUBQUERY here:

[NSPredicate predicateWithFormat:@"SUBQUERY(regions, $r, ANY $r.persons == %@).@count > 0", thePerson]];
1
  • Yep, that did it! Now I'm gonna go in a corner and try to figure out how it worked. Thank you!
    – Padin215
    Mar 13, 2013 at 16:10
2

To get all the Boundary entities that a Person has you can use an NSFetchRequest like this...

NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityName:@"Boundary"];

[request setPredicate:[NSPredicate predicateWithFormat:@"region.person = %@", thePerson]];

This will the return an array of Boundary entities whose Person is the one you put in the predicate.

Then you can do...

NSSet *states = [NSSet setWithArray:[results valueForKey:@"state"]];

Then states will be a set of unique states that the Person "owns".

With the many to many this changes.

You could do something along the lines of this for the predicate...

[NSPredicate predicateWithFormat:@"ANY region IN %@", thePerson.regions];

... I think this might work?

7
  • (You were faster, I have discarded my answer :-) - The last step can be simplified to NSArray *states = [results valueForKey:@"state"].
    – Martin R
    Mar 13, 2013 at 15:44
  • 1
    Ha! Seems kinda simple and obvious now, thanks! And I think I should have mentioned Boundary can have many Persons. So the predicate would then be @"Any region.person == %@", thePerson, right?
    – Padin215
    Mar 13, 2013 at 15:48
  • @Log139: If only one of the relationships person, region is a to-many relationship then this should work. If both are to-many then a more complicated predicate is needed.
    – Martin R
    Mar 13, 2013 at 15:55
  • Ya, its many-to-many relationships. A Person can have many Region, and a Region can have many Person. A Region can have many Boundary and a Boundary can have many Region.
    – Padin215
    Mar 13, 2013 at 15:58
  • Then you are stuck. You need a sub-predicate in that case. You also need to change your diagram in your question as it is incorrect and misleading.
    – Fogmeister
    Mar 13, 2013 at 16:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.