Reputation: 4173
The blue Table is called Routine
and the other one is called Exercise
.
Routine.exercise
has a Many-to-Many
relationship with Exercise.routine
.
I have a UITableView
that the user can re-arrange the order of the items and I am having problems thinking a way to design my Entities so that I can reorder them & save their reorder position.
What I want to order is the position in the UITable of the Exercises in X Routine.
E.g: The Routine A has been selected and it has the following items in the following order.
E1
E3
E2
My problem comes when I can not put an order column
in the Exercise entity because is a Many-to-Many
relationship. Thus, that exercise will have different position in different routines.
I also though on creating a different Entity that would hold the position data but then that would mean that I'd have to create X amount of column having in mind that the amount of columns have to be greater than the typical number of exercises that a routine could have. This is so far my most viable option but I was wondering if there is any better solution as there will be lots of rows that will be unused and while storing it will mean looping through all the empty ones and nullify them just in case.
Can anyone think a better way or is there a better way in Xcode to store position of item in UITable? Can i store an array in a field?
I am using CoreData.
EDIT in response to the answer:
Upvotes: 0
Views: 86
Reputation: 31016
A many-to-many relationship can be re-written as two one-to-many relationships with a join table in the middle. That is, create an ExerciseRoutine table (or some better name) that represents the matching of an Exercise and a Routine and put any data about the specific relationship in there.
The less flexible way -- since you currently only want to know the order of exercises in a routine -- would be to store the order as an array, connected to the routine.
(But I recommend option #1 as a better general purpose technique.)
Upvotes: 1