Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Model a tree structure in Core Data

I want to implement a multi-way tree using core data. There's a "Plan" entity in my core data model.

            Plan
           /  |  \
        Plan Plan Plan
     /  | \   |     |   \
   Plan Plan ...

The plan of the higher level will have to-many relationship with the sub-plans.

So how should I set the relationship?

enter image description here

enter image description here

like image 863
JackieLam Avatar asked May 19 '13 10:05

JackieLam


1 Answers

I would define

  • a to-many relationship subplans (or children) from the Plan entity to itself, with the "Delete Rule" set to "Cascade",
  • a to-one relationship superplan (or parent) from the Plan entity to itself, with the "Delete Rule" set to "Nullify",
  • and set these as inverse relationships of each other.

If you delete one Plan object, then automatically

  • all sub-plans are deleted due to the "Cascade" delete rule, and
  • the plan is removed from its parent object due to the "Nullify" delete rule.
like image 132
Martin R Avatar answered Oct 01 '22 03:10

Martin R