Re: NSDocument -canCloseDocumentWithDelegate::: not called when terminating
Re: NSDocument -canCloseDocumentWithDelegate::: not called when terminating
- Subject: Re: NSDocument -canCloseDocumentWithDelegate::: not called when terminating
- From: Kevin Perry <email@hidden>
- Date: Mon, 23 Jul 2012 09:06:13 -0700
I can't really recommend an approach that essentially lies about the "dirty" state of a document. If you have something to write into the document bundle, then the document is dirty and it should be declared so with the available NSDocument APIs. If the existing APIs don't meet your needs for whatever reason, file an enhancement request.
Also, NSDocument and NSFileWrapper (assuming you're using it) might not take it very kindly if you modify the package outside of the normal -save… and -write… APIs, especially if/when you adopt Autosave and iCloud.
To answer your question more directly—there is no public callback that is invoked in every case on Quit when the document returns NO from -isDocumentEdited or -hasUnautosavedChanges. In fact, sometimes nothing gets invoked at all because when -isDocumentEdited returns NO, NSDocument enables sudden termination, which allows -[NSApplication terminate:] to take shortcuts and Shutdown/Restart/Logout to kill the app.
-KP
On Jul 23, 2012, at 8:12 AM, Markus Spoettl <email@hidden> wrote:
> Hello,
>
> when I close my document window without terminating the application, -canCloseDocumentWithDelegate::: is called, independent of whether the document is dirty.
>
> When I quit the application, -canCloseDocumentWithDelegate::: is only called if the document reports it is dirty. If it is not dirty, AppKit takes a shortcut calls into -close directly. NSDocumentController doesn't seem to be involved either.
>
> Why do I need this?
>
> My document is stored in a package that contains a preferences file. When the document is saved, the preferences file is saved as part of the package. So, if it is dirty at the time of closing it's going to be saved. If the document isn't dirty, I'd like to update the preference file only.
>
> A good place would have been -canCloseDocumentWithDelegate::: because I can check whether the document is dirty and save the preferences file if necessary. That doesn't work if it's not getting called of course. -close is called after the document is saved, so the dirty flag there isn't helpful.
>
> Are there better options to do a last-minute save? I do not want to change the design, meaning I don't want preferences to contribute to the change count state or even worse be undoable. Plus I do want the preferences to be stored in the package, not someplace else (restoration state for example).
>
> Thanks for any pointers!
>
> Regards
> Markus
> --
> __________________________________________
> Markus Spoettl
> _______________________________________________
>
> Cocoa-dev mailing list (email@hidden)
>
> Please do not post admin requests or moderator comments to the list.
> Contact the moderators at cocoa-dev-admins(at)lists.apple.com
>
> Help/Unsubscribe/Update your Subscription:
>
> This email sent to email@hidden
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden