Search code examples
objective-cxcodecore-datansarraycontrollernsset

How to setup "Contens Set" in NSArrayController for Core Data with a one-to-many relationship?


I try to get Core Data working with a simple one-to-many relationship in Xcode. My 2 entities, employee and department, are setup in the data model, and relationships are created: Employee has a to-one relationship to department Department has a to-many relationship to employee.

My xib has 2 table views for each entity. I wan to maintain my data there, selecting a department should show all employees, and I can add one more by using the insert method of that controller. My controllers ( one for each entity) have set:

  • entity name
  • flag prepares content is set
  • Managed Object context is bound to my object context
  • reference binding to table view content is set to arranged objects

Now, I thought the idea is to set the "Content Set" in the array controllers to manage the relationships. Like so: Department.selection.employee ( for employee ) Employee.selection.department ( for Department )

But all I get is a BAD ACCESS crash..

Any idea of what is wrong is welcome !


Solution

  • Don't use any of the content bindings of the departments array controller to show all departments. Bind the content set of the employees array controller to Department.selection.employees. Department is the department array controller, employees is the to-many relationship.