Reputation: 1737
I have a problem with Core Data. The problem happens with this scenario :
-I have two entities, for instance "Department" and "Employee".
-A to-many relationship from "Department" to "Employee"
When I generate the model, two classes are generated : "Department" and "Employee". The "Department" class has an "NSSet" of employee thank's to the to-many relation.
In the code, I create two instance of "Department" (d1, d2) and one of "Employee" (e1). I would like that e1 instance could be in both d1 and d2 (via the relationship). So I do something like :
[d1 addEmployeeObject:e1 ];
[d2 addEmployeeObject:e1 ];
The problem is that, when I restart the application, e1 is no more in d1 (only in d2). It seems core-data automatically removed e1 from d1. But I don't want that !!!
According to the core-data documentation, it seems that it is the appropriate behavior ( read Manipulating Relationships and Object Graph Integrity chapter in the link above).
How can I prevent this behavior ?? Any Idea ?
Thanks for reading, really hope I have been clear.
Upvotes: 1
Views: 171
Reputation: 3827
Make an inverse relationship called Departments, a to-many from Employee to Department.
Mark the inverse of "Departments" as "Employees" where "Employees" is the to-many from Department to Employee.
When you generate the classes you should see that Department has an NSSet *employees and Employee has an NSSet *departments.
Upvotes: 2