2

I need to perform some cleanup operations in my NSDocument subclass when documents are closed.

With the current implementation having that cleanup code in canCloseDocumentWithDelegate:shouldCloseSelector:contextInfo: it only gets called when the document has any unsaved changed.

Even the close method doesn't look like a good place - according to the docs This method may not always be called.

Is there one place to put that kind of code that's always called before a NSDocument instance is destructed..?

There's some really neat message flow diagrams for initializing, opening and saving NSDocuments in the docs - alas none for closing documents or quitting the app?!

Jay
  • 6,572
  • 3
  • 37
  • 65

1 Answers1

0

Your NSDocument lives in a window.

Why not set some object (e.g. your subclassed NSDocument object) as a delegate for the NSWindow that hosts the document and then catch the "windowWillClose:" notification?

Michael Dautermann
  • 88,797
  • 17
  • 166
  • 215
  • 1
    I could think of a dozen workarounds - I was however wondering what's the official, supported way in the `NSDocument` class... in an application framework that's been around for decades which surely has built-in support for such a basic use case :-) – Jay Jan 22 '14 at 05:42