Reputation: 280
I have a Core Data database containing (among others) WorkingPlan and Position. A WorkingPlan has zero or more Position objects. Everything works fine, until I am trying to add the same Position to one WorkingPlan multiple times. Instead of having multiple relations (as I would need it to be), i only get one relation. What can I do to get multiple relations to that Position?
Upvotes: 1
Views: 2807
Reputation: 539685
As already mentioned in the comments, a Core Data relationship describes the relations of one object to a set of distinct other objects. It is not possible for one object to have more than one relationship to the same other object.
One possible solution that comes into my mind is to introduce another entity WorkingStep
between WorkingPlan
and Position
:
steps
is a ordered to-many relationship from WorkingPlan
to WorkingStep
,plan
is the inverse to-one relationship,position
is a to-one relationship from WorkingStep
to Position
,steps
is the inverse to-many relationship.I have suggested an ordered relationship from WorkingPlan
to WorkingStep
, because I assume that the steps for one plan must be executed in a defined order.
For example, if the positions "pos1", "pos2", "pos1" have to be done for a plan in that order, you would add 3 steps "step1", "step2", "step3" to the plan, and both "step1" and "step3" are related to "pos1", and "step2" is related to "pos2".
Adding values to an ordered relationship is a bit tricky. The following code shows how to create the objects described above:
WorkingPlan *plan1 = [NSEntityDescription insertNewObjectForEntityForName:@"WorkingPlan" inManagedObjectContext:context];
WorkingStep *step1 = [NSEntityDescription insertNewObjectForEntityForName:@"WorkingStep" inManagedObjectContext:context];
WorkingStep *step2 = [NSEntityDescription insertNewObjectForEntityForName:@"WorkingStep" inManagedObjectContext:context];
WorkingStep *step3 = [NSEntityDescription insertNewObjectForEntityForName:@"WorkingStep" inManagedObjectContext:context];
Position *pos1 = [NSEntityDescription insertNewObjectForEntityForName:@"Position" inManagedObjectContext:context];
Position *pos2 = [NSEntityDescription insertNewObjectForEntityForName:@"Position" inManagedObjectContext:context];
step1.position = pos1;
step2.position = pos2;
step3.position = pos1;
// temporary proxy object used to modify the ordered to-many relationship "steps":
NSMutableOrderedSet *tmpMutableSteps = [plan1 mutableOrderedSetValueForKey:@"steps"];
[tmpMutableSteps addObject:step1];
[tmpMutableSteps addObject:step2];
[tmpMutableSteps addObject:step3];
More information:
You can also set plan1.steps
in one "step" like this:
plan1.steps = [NSOrderedSet orderedSetWithObjects:step1, step2, step3, nil]; // works!
But some accessor methods do not work with ordered relationships:
[plan1 addStepsObject:step1]; // does not work!
seems to be the "logical step", but it throws an NSInvalidArgumentException
:
*** -[NSSet intersectsSet:]: set argument is not an NSSet
This seems to be a bug in the auto-generated accessor methods which has already been noticed by other people (e.g. Exception thrown in NSOrderedSet generated accessors). Using the proxy object is a workaround for that problem.
Upvotes: 5
Reputation: 3585
In the xCode Core Data model add a relationship, "positions" to WorkingPlan. With the working plan object still selected, and the "positions" relationship selected, in the relationship properties form on the data model inspector (on the right hand side of xCode) select the "plural" "To-Many Relationship" checkbox. If you need to have a precise number of positions related to workingPlan, fill in the minimum/maximum count fields as required.
Upvotes: 0