I need to create a Core Data entity that has a one-to-many relationship to itself (think of a folder containing subfolders: i need to access all subfolders of a certain folder as well as the parent of any subfolder).
-
Is there an easy way to do this using standard Core Data relationships, without an auxiliary one-to-many entity?
-
I guess I can accomplish this easily “the old way” by using my own key field – but is there an easy way to define an auto-incrementing field in Core Data? (I can use a time-based value rather than an auto-incrementing one, but I don’t like this solution for several reasons).
Core Data doesn’t have fields. Don’t try to think of Core Data is terms of a procedural, non-object API like SQL. You’ll just confuse yourself.
It is trivial to create a nested relationship in Core Data. To make an entity to model a directory structure, all you would need is:
Any particular folder has one parent and multiple children.
Don’t mistake the entities in the data model for the graph of live objects. The entities in the data model just define abstract attributes and relationships of equally abstract entities and can be very simple. The actual live graph is as complex as the phenomena it models.
E.g. In this case, the data model would be as simple as that above, one entity, with one attribute and two relationships all to itself. The live graph would be as complex as the file system it modeled with thousands of unique
Folderobjects and millions of relationships.