CoreData: sorting and section by relationship

I have two questions about using Core Data. As an example, consider a typical Employee and Department case. The employee has a relationship field of "deparment" to one-one to Department, and the unit has a relationship of "employee" as many for the Employee person.

I would like to display all the employees in TableView in the department name sections. I think the NSFetchedResultsController should use Employee as an entity. I am not sure how to use the department name as a sorting criterion, since it is in the field field of the employee relations department. Can you use "DepartmentName" as a sort and add this to the Employee entity class as a method that will return the department name for this employee?

The second question is the name of the section. I would like to use department names as partitions. Can I use the above method as the sectionKeyPath value for NSFethedResultsController?

Not sure I'm on the right track.

+5
source share
1 answer

You can set the sort key as department.name for sectionKey as well as for sort. You can even sort by department.name first, and then by employee name.

Update

The string "department.name" can be used as the sort key in NSSortDescriptorand as the name of the section in the build NSFetchedResultsController.

This is part of the coding of key values ​​and I highly recommend that you read about it, as there is a lot of energy and flexibility.

+3

All Articles