NSTreeController addChild: does not call newObject but add: does (Class/Object mode)

365 views Asked by At

I noticed that my subclass os NSTreeController runs newObject only when the add: method is called but never for addChild:

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 method?

If 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 add: and addChild: - again only add: calls newObject. I find this really really really weird behavior.

2

There are 2 answers

1
iluvcapra On

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.

If 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.

1
CodeSmile On

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.