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?
I have a Core Data database containing (among others) WorkingPlan and Position . A
Share
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
WorkingStepbetweenWorkingPlanandPosition:stepsis a ordered to-many relationship fromWorkingPlantoWorkingStep,planis the inverse to-one relationship,positionis a to-one relationship fromWorkingSteptoPosition,stepsis the inverse to-many relationship.I have suggested an ordered relationship from
WorkingPlantoWorkingStep, 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:
More information:
You can also set
plan1.stepsin one “step” like this:But some accessor methods do not work with ordered relationships:
seems to be the “logical step”, but it throws an
NSInvalidArgumentException: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.