Getting a separate list of identifiers in CoreData

What is the most efficient way to execute this query in a CoreData table? To use the standard employee database model, I want the DISTINCT department identifier for all departments that contain employees with job descriptions for the chef. As it happens, there is only one table (Employees) - I actually do not have a partition table, but only the department identifier, which is repeated.

+3
source share
1 answer

Given the described scheme, I would fetch with a predicate (formatted string), for example @"jobDescription LIKE 'chef'", and then use key coding to get unique values ​​from the resulting array:

[result valueForKeyPath:@"@distinctUnionOfValues.departmentID"];

or create a set:

NSSet *deparmentIDs = [NSSet setWithArray:[result valueForKey:@"departmentID"]];

Depending on the size of the problem (how many employees), the last step in memory may be prohibitive. At this point, you will need to create a unit and do the work to make sure that you have connected the appropriate employees to each department. Then you can make a selection of departments with a predicate (format string), for example @"ANY employees.jobDescription LIKE 'chef'", to get departments with cook employees.

+5
source

Source: https://habr.com/ru/post/1761795/


All Articles