1

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?

cLar
  • 260
  • 1
  • 3
  • 17
  • 2
    You cannot have multiple relationships to the same object in Core Data. (The value of a relationship is a `NSSet`, which contains a set of *distinct* objects.) – Martin R Jan 11 '13 at 16:43
  • I was afraid to hear that. Is there any other way to achieve this? – cLar Jan 11 '13 at 17:18
  • 1
    Why do you want to add the same object multiple times? Can you explain what a bit? - You could have multiple Position objects with the same attributes, if that helps. – Martin R Jan 11 '13 at 17:22
  • The positions are basically steps to do to complete the WorkingPlan. Some of these steps have to be done more than once in this process. Unfortunately, the data comes from a company's database via a webservice so i cannot change anything. – cLar Jan 11 '13 at 20:30
  • I have tried to figure out something in my answer. Don't know if that is a possible solution for you. – Martin R Jan 11 '13 at 21:39

2 Answers2

5

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:

enter image description here

  • 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.

Community
  • 1
  • 1
Martin R
  • 488,667
  • 78
  • 1,132
  • 1,248
  • very nice solution, thank you! I still have one question left: With ordered relationships, do I have to modify the NSOrderedSet via the temporary object or could i write directly to `plan1.steps`? – cLar Jan 12 '13 at 09:11
  • 1
    @cLar: You are welcome! - I have added some information on how to modify an ordered relationship. – Martin R Jan 12 '13 at 10:05
  • Thanks a lot for your help! – cLar Jan 12 '13 at 10:26
0

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.

TheBasicMind
  • 3,425
  • 1
  • 16
  • 20
  • Thank you, but that was not exactly what i meant. I have already set the relationship to be a "To-many Relationship", but i need to add the _exact same object_ multiple times. – cLar Jan 11 '13 at 20:35