开发者

Getting distinct list of ID's in CoreData

What's the most efficient way to perform this query on a CoreData table? To use the standard employees database model -- I want DISTINCT department ID's for all departments that contain employees with job-description "chef." As it happens, there is only a single table (Employees) relevant here -- I don't actually have a departments table开发者_如何学C, just department ID's that are repeated.


Given the schema you describe, I would execute a fetch with a predicate (format string) like @"jobDescription LIKE 'chef'" and then use key-value coding to get the 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), doing the final step in-memory may prove prohibitive. At this point, you'll have to create a Department entity and do the work to make sure you connect appropriate employees to each department. Then you can do a fetch of Departments with a predicate (format string) like @"ANY employees.jobDescription LIKE 'chef'" to get the departments with chef employees.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜