Challenge: Why MyDocument instead of CarArrayController?


First of all, I love your book.

The challenge was hard - I ended up having to download the code.

When I downloaded the code I was surprised to see the tableView outlet and the createCar: action in MyDocument.h. The hint in the book said you would have to add the outlet to CarArrayController, which made sense to me because CarArrayController was the controller that was attached to the managed objects. Adding carsController to MyDocument.h didn’t seem to make sense, because it looked like you would have two separate objects (the NSArrayController ‘Cars’ and the carsController in MyDocument) possibly controlling the content of the table. It didn’t make sense to have two different controllers managing the same content, and I even thought it might lead to conflict problems.

Anyway, because the hint said to add the outlet to CarArrayController, I spent a lot of time spinning my wheels trying to do that, to no avail.

So then I downloaded the solution and saw how you had done it. I added the code to my project they way you did it and it worked (of course). But then I got to thinking - couldn’t you do the same thing by adding the tableView outlet and createCar: action in CarArrayController? I tinkered with it, and eventually got it to work.

And having the createCar: action and tableView outlet in CarArrayController actually seemed to make more sense. The custom newObject: action is defined there. The add: action was invoked through CarArrayController before it was replaced by createCar:. CarArrayController already controlled the content of the table. It was counter-intuitive and confusing to see those changes in MyDocument instead of CarArrayController.

Here is what it boils down to:

  1. Is there a reason why you put the tableView outlet and createCar: action in MyDocument? Is there some reason why it works better there, instead of in CarArrayController? I like it better in CarArrayController, just cuz that’s where I would expect to find custom code that modified the way the content of the table was being handled - after all, the content of the table is bound to the controller, right? But if there’s some reason why MyDocument is a better place for the code, I would love to know why…I think it would help cement some of the concepts for me.

  2. If you want people to put the tableView outlet in MyDocument, you should change the hint accordingly.


I got the createCar action to work as an action in the CarController by adding that action and a tableView outlet and using self for the controller. The only issue was that undo was a bit funky because the car controller does not have access to the documents’ undoManager so the current undo group does not get closed prior to creating the new object.

It would seem that object creation is best done in the document class because of the need to access the documents undo state. Appears that the same undo manager could be accessed from table views’ windows’ undoManager. Not very obvious that these would be the same, however.