views:

38

answers:

2

I have a very simple problem that i'm sure has a simple solution, but after searching the internet for 2 days I can't find anything on it =(

Here is the situation: I have decided to break my data into 2 sperate entities with a one-to-one relationship. So I have the PERSON entity and FEATURES entity. each PERSON has a related FEATURES entity with a person_id pointing back to PERSON.

So how do I fetch data from the FEATURES related to a specific PERSON? I haven't found any Predicate related to relationship id's

The only thing I can think of is to create a person_name attribute on FEATURES and then do this: predicateWithFormat:@"person_name = 'Bill'"

but doesn't that defeat the purpose of setting up the relationships?

So how do I fetch based on relationships??

+3  A: 

It sounds like you come from a relational database background. You should be storing the relationship from FEATURE to PERSON in a relationship, not an attribute (you may already be doing this but your naming person_id makes me suspicious; the standard Core Data convention would be to name the relationship person and its inverse feature). Assuming this is the case, you can use a predicate format like

NSManagedObject *person ... //the person instance you want to search against
NSPredicate *p = [NSPredicate predicateWithFormat:@"self.person == %@", person];

Of course, you should be defining the inverse relationship (PERSON=>FEATURE) in which case you can just do

NSManagedObject *feature = person.feature;

assuming you name that relationship feature. Core Data uses inverse relationships to maintain consistency of the object graph. Their storage is minimal and the benefits to having them are significant; without a really good reason, you should always define inverse relationships for all relationships in a Core Data model.

Barry Wark
thanks for the reply, but i'm still not getting it :(I'm able to get the PERSON entity just fine using this line:NSManagedObject *person = [fetchedResultsController objectAtIndexPath:indexPath];from there, I can easily pull out any attribute on PERSON related to that object. But for the life of me, I can not figure out how to access the FEATURE entity related to the given person.I tried: NSManagedObject *feature = person.feature, but i get errorsAlso tried NSPredicate *p = [NSPredicate predicateWithFormat:@"self.person == %@", person]; but it gives a "unrecognized selector"
I'm also not sure if its supposed to be self.person or self.PERSON
A: 

ok I think i figured it out. I need to explicity set the relationship id when i create the entity object, which i wasn't doing. I thought core data somehow managed that behind the scenes or something.

[features setValue:personObject forKey:@"features"];