This question is in the context of Core Data, but if I'm not mistaken, it applies equally to the more general case of SQL.
I want to save an ordered table using Core Data, with the ability for the user:
- reorder lines
- add new lines anywhere
- delete existing row
What is the best data model for this? I see two ways:
1) Model it as an array: I add an int position property to my entity
2) Compare it as a linked list: I add two one-to-one relationships, next and previous from my object to myself
1) simplifies sorting, but it is painful to insert or delete, because you need to update the position all objects that appear after
2) makes it easy to insert or delete, but it is very difficult to sort. Actually, I donβt think I know how to express a sort descriptor (SQL ORDER BY ) for this case.
Now I can present an option on 1):
3) add an int ordering property for the entity, but instead of counting it one by one, specify a score of 100 per 100 (for example). Then inserting is as simple as finding any number between the order of the previous and next existing objects. Expensive numbering should only happen after filling 100 holes. Creating this float instead of int makes it even better: you can almost always find a new float halfway between two floats.
Am I on the right track with solution 3), or is there something smarter?
database html-lists database-design core-data datamodel
Jean-denis muys
source share