Search code examples
ioscore-datamany-to-many

Many to many relationship to self in Core Data (iOS)


In my project, I have an entity, Task. I was going to create another entity, Subtask, and assign a one to many relationship from Task to Subtask, meaning one Task can have many Subtasks.

However, upon mapping it out, I've realized that the Subtask entity is identical to the Task entity, as far as its properties. They are the same, only difference is one is a parent of the other.

Would it be better to create a one to many relationship with no inverse, (subTask property) as shown:

enter image description here

Or would it be better to create another object, and set the relationship as so:

enter image description here

Or would it be better to create a parent property and a child property in the Task entity, and set the relationship as so:

enter image description here

I think the last method is the best but I am not sure, any direction would be wonderful.


Solution

  • If the two entities would be identical except for the parent/child relationships, then a to-many relationship from an entity to itself is the way to go. No sense creating two copies of the entity when one will do. If those two entities are (or might ever become) different in any other way, keep them separate.