stifin
stifin

Reputation: 1400

How to retrieve a specific object through a relationship in core data

Say I have 2 managed objects in my model: Department and Employee (as discussed in the Core Data Programming Guide). If I already have a specific department retrieved, I know I can get all employees in that department through

NSSet *departmentsEmployees = aDepartment.employees;

but what if I want to find a specific employee (e.g. with employeeId = 123) in that set, change one of its attributes, then save the change? How do I do that? Can I do a targeted query on the set? Or would I have to loop through each employee to find the one I want?

It seems that it would be better to try to find it in the employees NSSet instead of doing a whole new query to the entire data model because I already have a specific department.

Thank you

Upvotes: 0

Views: 217

Answers (1)

Caleb
Caleb

Reputation: 124997

One way to narrow the search is to use -[NSSet filteredSetUsingPredicate:].

Upvotes: 1

Related Questions