Q:

Core Data: To-Many Relationships with Repeat Values

In a mileage-tracking app I am developing, the concepts include "Places," "Legs," and "Routes."  Essentially, a Route is a collection of connected Legs, where each Leg is simply a straight line between two Places (with an associated distance).
I am having a problem modeling the Route-Leg relationship in Core Data because a Route may include the same Leg twice or more.  For example, a person could travel Home to Office, Office to Worksite, Worksite to Home, Home to Office, and Office to Home as a single, contiguous path of travel on a given day.  In that case, the "Home-to-Office" Leg is contained twice in the Route.  But Core Data creates the To-Many relationship in a NSManagedObject subclass as an instance of NSOrderedSet, which does not permit duplicate Legs.
Is there a way I can create a relationship between the two Core Data-backed objects (Route and Leg) that allows multiple occurrences of the same Leg in a single Route?  I am developing my app in Swift, so any Swift-specific suggestions would be appreciated.
Thank you.

A:

Presumably you currently have a many-many relationship between Leg and Route.  One solution is to replace this many-many relationship with an intermediate entity (say RouteLegDetails) which has to-one relationships with Leg and Route.  Each Route can have many RouteLegDetails and likewise each Leg can have many RouteLegDetails.  In the data model editor, it might look something like this:

This way, if you have a Leg that is traversed twice (or more) on the same Route, each instance is represented by a different RouteLegDetails: which overcomes the restriction on duplicates in the legDetails ordered set.
This will complicate your code slightly: when you want to add a Leg to a Route, you will have to create a RouteLegDetails object, and add it to both the legDetails relationship on the Route and the routeDetails relationship on the Leg.  Conversely to remove a Leg from a Route, you will need to delete the corresponding RouteLegDetails object.

