I have an issue I am trying to solve where I need to have one object relate to another object more than once. Currently, I have a one to many relationship between “Sequence” and “Pose”. When you create a sequence, you add poses a batch at a time to the sequence. The code looks something like this:
- (void)doneAddingItems {
NSMutableOrderedSet *saveSet = [NSMutableOrderedSet orderedSetWithOrderedSet:self.sequence.poses];
[saveSet addObjectsFromArray:self.posesToAdd];
self.sequence.poses = [NSOrderedSet orderedSetWithOrderedSet:saveSet];
[self.managedObjectContext save:nil];
[self.navigationController dismissViewControllerAnimated:YES completion:nil];
}
This works great for a list of unique “poses”, however obviously you can’t have a relationship to the same object TWICE so I can’t make a sequence like the following:
PoseA -> PoseB -> PoseC -> PoseA -> PoseD
How are others working with this? I was thinking of creating a dynamic entity that will check for the existence of a selected Pose in the sequence already, and if it exists, duplicate that Pose so we can “reference it twice”. They will both contain the same info, but will be at different indexes. I am thinking this could solve my issue, and would only require me to add a dynamic entity and some code to validate duplicates and clean up the entity as sequences are removed.
Does anybody have a better idea?
I think a new NSManagedObject class of type Index could solve your problem. Instead of having a relationship between Sequence and Pose you have a 1:many relationship between Sequence and Index, then have a many:1 relationship between Index and Pose. You can recover a complete sequence of poses by retrieving Indexes ordered by an “order” attribute. Each Index refers to a specific Pose while each Pose can be used in multiple Indexes. Index is a very lightweight object with just the “order” attribute and relationships.
A side effect is that you no longer need to use an ordered set because the “order” value of Index captures that information.
(choreography app? photography?)