Once my core data model is set up, how do I then instantiate objects that follow the hierarchy tree?

I know how to trigger the event itself, how to, for instance, fill out a form and have the user click button and create a managed object.


What I'm not sure about is how to ensure that objects get created in the right "branch" of the tree.


So for instance, I have multiple projects. Each project can have many ObjectA and many ObjectB, and each ObjectA can have multiple ObjectB. ObjectA may have only one Project, and ObjectB may have only one ObjectA or one Project.


  • Project1
    • ObjectA
      • ObjectB
      • ObjectB
    • ObjectA

      ObjectB

  • Project2
  • ObjectB
  • ObjectB


I need to be able to instantiate an ObjectA, for instance, under one specific project. I'm not sure how to identify that project. Likewise for ObjectB.


For the bonus, I also need to be able to later click and drag ObjectB into an ObjectA if needed. But right now I'm focused on just getting the tree of objects built and learning how to organize the objects themselves according to the given model.


I am using NSOutlineView to visually represent the objects, and I am using a TreeController to link the OutlineView to the Core Data managed objects. I think that is how it is supposed to work. The book I'm working with doesn't go into very much detail on the actual implementation of all of this. If anyone can suggest a current, thorough book that that does more than just lay out the language itself (like the Apple Swift books do...) that would be awesome. Asking questions in the forum is my last resort.

First, you'll have a better chance of getting answers if you move this question to the Core Data or Cocoa sections of the forum. The Core Data and Cocoa sections are in the App Frameworks section of the developer forums.


To answer your question about what project an ObjectA belongs to, add relationships to your Core Data model. Your Project entity would have a relationship for its ObjectA items. The destination for this relationship is ObjectA. Your ObjectA entity would have a relationship for its project whose destination is Project. Each relationship is the inverse of the other relationship.


The relationships for ObjectA and ObjectB are similar. Create a second relationship for ObjectA for its ObjectB items. Create a relationship for the ObjectB entity for the ObjectA it belongs to. These two relationships are the inverses of each other.


In code the relationships are just variables. The project relationship for Object A would look something like the following:


@NSManaged var project: Project


The name of the variable is the name of the relationship. The relationship for Project would look similar to the following:


@NSManaged var objects: Set<ObjectA>


When you create an ObjectA, set the project variable to the desired project and add the object to the project's collection of objects.

Once my core data model is set up, how do I then instantiate objects that follow the hierarchy tree?
 
 
Q