Reputation: 31
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??
Upvotes: 2
Views: 640
Reputation: 31
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"];
Upvotes: 1
Reputation: 107754
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.
Upvotes: 3