I have a Core Data design problem, I’m new to it and I’m still studying the Apple guide.
I have two entities: “BOX” (attributes: name, color, orderingValue) and “CD” (only attribute: name).
It is a many – to many relationship.
On my hypotetical desk I can have many BOXes.
A BOX can contain many CDs. Copies of the same CD can be stored in different BOXes on the desk.
I sort the BOXes using an orderingValue attribute so they can exchange place on the desk.
The problem is with CDs.
I’d like to be able to rearrange the order of the CDs in each one of the BOXes. I can’t use an orderingValue attribute for the CD entity as I do with BOX because if I set it to “5”, that CD will be at the place number 5 in each one of the BOXes and this is not what I want.
I thought using Ordered relationships but I discarded this option ’cause it is not yet compatible with iCloud which I’d like to support.
With a standard database I’d create a third table with BOX.Id, CD.Id and orderingValue and I’ll use it with a JOIN for the sorting.
What is the best option to handle this scenario in CoreData without using ordered relationships?
Thanks
Nicola
Update: I followed codeghost’s advise, it works flawlessly!
I followed codeghost’ advise:
1) I created a third entity which I called “LinkedCD” with only one attribute: orderingValue;
2) I created a one-to-many relationship from the entity “Box” to “LinkedCD”;
3) I created a one-to-many relationship from the entity “Cd” to “LinkedCD”.
I can now sort independently the CDs for each one of the Boxes.
When I add a CD to one BOX I insert a new LinkedCD entity in CoreData and I compute the starting orderingValue which I update each time the CD is moved inside the BOX UITableView. When the UITableView is loaded I fill it with all the LinkedCD.Cd.name sorted by LinkedCD.orderingValue.
Everything works flawlessly.
Nicola