问题描述
我有一个基于文档的应用程序,我有子类 NSDocument
并提供了必需的方法,但我的文档需要一些广泛的清理(需要运行外部任务等)。哪里是最好的地方放这个?我尝试过几种不同的方法,例如:
-
关闭
-
close:
-
canCloseDocumentWithDelegate:shouldCloseSelector:contextInfo
-
dealloc
我把它放在 dealloc
,有时它会被调用,其他时候它不会(按Command + Q似乎绕过我的文档的释放),但是强制这个代码
将每个文档作为观察者添加到本地通知中心, code> NSApplicationWillTerminateNotification 。在其通知方法中,调用其清理方法(您也应该从 dealloc
或 close
)调用。 / p>
I have a document-based application and I have sub-classed NSDocument
and provided the required methods, but my document needs some extensive clean-up (needs to run external tasks etc). Where is the best place to put this? I have tried a few different methods such as:
close
close:
canCloseDocumentWithDelegate:shouldCloseSelector:contextInfo
dealloc
If I put it in dealloc
, sometimes it gets called and other times it does not (pressing Command+Q seems to bypass my document's deallocation), but it is mandatory that this code gets called without failure (unless program unexpectedly terminates).
Have each document add itself as an observer in the local notification center for NSApplicationWillTerminateNotification
. In its notification method, call its clean-up method (which you should also call from dealloc
or close
).
这篇关于你在哪里放置NSDocument子类的清理代码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!