I noticed that my subclass os NSTreeController runs
newObject only when the
add: method is called but never for
I found this thread discussing the very same issue. Though in this case the user is using the tree controller in Core Data mode, however I have mine set to use a specific Class (object mode).
What reason could there be for
addChild: not using the
newObject gets called for
add: I see no reason why it shouldn't work for
addChild:. I confirmed this behavior in Apple's SourceView demo app by replacing OutlineController with a subclass, and connecting buttons to
addChild: - again only
newObject. I find this really really really weird behavior.
Found the answer in the docs:
The add: and insert: actions use the newObject method to create the object that is added to the collection. In object mode the addChild:, and insertChild: create objects of the class specified by objectClass, but do not use the newObject method to do so.
At least it says that's the way it is. Though it contradicts the Class Reference. And they neglected to explain why it is the way it is.
What reason could there be for addChild: not using the newObject method?
It depends on context. If the
NSTreeController is talking to Plain Old Objects, it will run
-newObject in the context of
-addChild, because given the known
-objectClass, the tree controller has all the information it needs in order to put everything in the right place.
NSTreeController is talking to an
NSManagedObjectContext, it will not run
-newObject, because the
-newObject implementation won't have enough context to establish the relationship with the selection, and the relationship must be in place once
-addChild: is finished.