views:

144

answers:

1

Okay, I'm fairly new to Cocoa and Objective-C, and to OOP in general.

As background, I'm working on an extensible editor that stores the user's documents in a package. This of course required some "fun" to get around some issues with NSFileWrapper (i.e. a somewhat sneaky writing and loading process to avoid making NSFileWrappers for every single document within the bundle). The solution I arrived at was to essentially treat my NSDocument subclass as just a shell -- use it to make the folder for the bundle, and then pass off writing the actual content of the document to other methods.

Unfortunately, at some point I seem to have completely screwed the pooch. I don't know how this happened, but closing the document window no longer releases the document. The document object doesn't seem to receive a "close" message -- or any related messages -- even though the window closes successfully.

The end result is that if I start my app, create a new document, save it, then close it, and try to reopen it, the document window never appears. With some creative subclassing and NSLogging, I managed to figure out that the document object was still in memory, and still attached to the NSDocumentController instance, and so trying to open the document never got past the NSDocumentController's "hmm, currently have that one open" check.

I did have an NSWindowController and NSDocumentController instance, but I've purged them from my project completely. I've overridden nearly every method for NSDocument trying to find out where the issue is. So far as I know, my Interface Builder bindings are all correct -- "Close" in the main menu is attached to "performClose:" of the First Responder, etc, and I've tried with fresh unsullied MainMenu and Document xibs as well.

I thought that it might be something strange with my bundle writing code, so I basically deleted it all and started from scratch, but that didn't seem to work. I took out my init method overrides, and that didn't help either. I don't have the source of any simple document apps here, so I didn't try the next logical step (to substitute known-working code for mine in the readfromurl and writetourl methods).

I've had this problem for about sixteen hours of uninterrupted troubleshooting now, and needless to say, I'm at the end of my rope. If I can't figure it out, I guess I'm going to try the project from scratch with a lot more code and intensity based around the bundle-document mess. Any help would be greatly appreciated.

A: 

Hard to tell without code but I would suggest sending:

closeAllDocumentsWithDelegate:didCloseAllSelector:contextInfo:

... to the document controller and then looking at the controller as it is passed to the delegate to see how its state changes.

If the controller closes the document when you send the explicit message then your problem is with the binding to the window.

TechZen
Thanks -- I'll look into that as soon as possible. Yeah, I know code would be helpful :( I considered just zipping the whole damn thing and putting it on my server. I guess I was hoping against hope that it was just a simple idiocy on my part.Anyway, I'll check out that possibility and get back. Thanks for your help :)
Nathan Douglas
Yep, apparently a binding problem in the interface. As much as I appreciate the ease of Interface Builder, I don't think it's the right tool for me now. I need something where I can see the code. Kind of a problem with Cocoa in general, since so much of the code is stuff that goes on without me really understanding it.I guess I have a hell of a lot of work to do before this starts making sense.Thanks for your help!
Nathan Douglas
Yes, that is a very common problem. I have been working with these tools for 13 years now and they still trip me up. It's a tradeoff of ease of use versus internal complexity. Having IB do all this stuff for you automatically is great but if it breaks, its hard to know where it broke without prior experience.
TechZen