你在哪里放置NSDocument子类的清理代码? [英] Where do you put cleanup code for NSDocument sub-classes?

查看:111
本文介绍了你在哪里放置NSDocument子类的清理代码?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个基于文档的应用程序,我有子类 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子类的清理代码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆