1

I am using core data framework and want to fetch the data using relationship. I have 2 entities named User and Company having user_name and company_name field's. I also have relationship to-many name rel_user->user.

Now I am trying to fetch data like this

User *userObj=(User*)[selectedData objectAtIndex:indexPath.row];
NSSet *resultData = [userObj valueForKeyPath:@"rel_user.user"];
    NSLog(@"subject -> %@",[resultData description]);

I want to show the company name related to user.

2 Answers 2

1

What is the name of the inverse relationship to rel_user? Assuming it's rel_company and the user -> company relation is to-one, you'd just use

user.rel_company.company.name

Where user is a particular User object.

There isn't really any benefit to specifically naming your relationships rel-something. It reads much nicer to have a company relationship on the User (since it will be a property holding the Company) and a users relationship on the Company. You can then tell from the name what the property will contain and if it is a to-one or to-many relationship.

Sign up to request clarification or add additional context in comments.

2 Comments

Hello Thank you for replying but I can't find the relationship property with the User object. How can I get that do I need to set parent class for that??
You define the relationships yourself in the core data model editor. If you don't create subclasses, you'll have to use valueForKeyPath.
0

you can fetch related company name by relation name.

User userObj=(User)[selectedData objectAtIndex:indexPath.row];

NSString *companyName = userObj.RELATION_NAME.company_name

RELATION_NAME is used to connect from user entity to company entity.

Hope this will help.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.