This should be easy, how difficult can it be.
I have a document based core data application with a very simple data model.
I have a "node" entity with a parent/children relationship to itself controlled by a NSTreeController, and viewed through an NSOutlineView. The "node" also has a non optional (to one) relationship to another entity type "nodeProperties" which is managed by a NSArrayController. I have NSManagedObject sublasses for both of the entities. My document class has outlets bound to both the tree controller and array controller instances.
My problem is how to ensure that, when a new "node" is created by a user interface action in the outline view, its relation to a suitable (pre-existing) nodeProperties object is populated.
Approaches I have tried / considered:
Let the tree controller create the "node" (from its add:, addChild: actions) and populate the relationship to a nodeProperties object in the "node" subclass awakeFromInsert method. The trouble is I cannot find a means of accessing any nodeProperties object from within the "node"s awakeFromInsert. The "appropriate" nodeProperties object is available from a method in the document class, but accessing the document object from the node awakeFromInsert method seems to break the principles of MVC, and I have read that the shared document object is not always safe in a drag and drop operation (which in my case also creates a new node object)
Write add: and addChild: action methods in the document class and invoke these from the end user actions instead of the tree controller (My drag and drop support is also in the document class). Then from within these methods invoke the add: and addChild: methods in the tree controller, then set the nodeProperties relationship on the newly created node. The trouble is I don't know how to ask the tree controller to give me a reference to the newly created node? I have tried using the selectedObjects method to get the parent, and then comparing the parents children before and after the add to get the new node. But the children content does not change at this time - perhaps it is a delayed update?
As a variant of 2, don't use the tree controller add:/addChild: methods at all, but instead create the node entity object in the document add:/addChild: methods using the tree controllers selectedOjects to get the parent. I don't really like this since it seems like doing something behind the tree controllers back, and I would have to setContent: each time I created root objects.
I have considered the possibility of observing the creation of the newly created node, but I don't know what to observe to achieve that.
Someone must have done something like this before - but I trawled to no avail. All help, advice, guidance would be very welcome.