Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Core Data: How to design a tree data structure from one core data entry

I am struggling with designing a coreData model where I have only one type of entry called "To-Do". Each To-Do entry has either 0, 1, 2, ... , or n relationships to other (sub) entries just like To-Do. So the relationships between the To-Do entries design a tree structure with an undefined number of child nodes. The following graphic should illustrate the case (E = core data entry):

            E                            
           /|\                        
          / | \                       
         E  E  E                     
        / \               
       /   \         
      E     E               
     /|\                   
    E E E          

My guess was to model that data like illustrated in the following graph. I didn't choose the inverse relationship because Xcode made a many-to-many relationship out of it which doesn't match the tree design.

enter image description here

Also I saw in the data model inspector something called "parent entry". So I started to believe I might have to create a second entry named "To-Do-Child" with the same attributes and make the other entry to the parent entry. The manual tells me that this might be the wrong path to go...

Questions:

  1. How can I model this approach within the core data model file? Is one of the ones mentioned correct?

  2. How will I be able to fetch all To-Do entries of a specified parent node? Since they arise from the same entry I have problems to address the exact To-Do subtree I want.

like image 222
d.ennis Avatar asked Oct 09 '22 04:10

d.ennis


1 Answers

I think you need a relationship of parent (destination entity is your to do entity) which serves as the destination for the inverse relationship.

Entries at the top of the tree have nil value for this relationship.

For any to-do item, the set returned from the childToDos relationship will hold all the children. It doesn't matter that these are of the same class.

like image 157
jrturton Avatar answered Oct 13 '22 10:10

jrturton