如何确保在IOS 5中使用ARC破坏单例? [英] How to ensure destruction of singleton in IOS 5 with ARC?

查看:138
本文介绍了如何确保在IOS 5中使用ARC破坏单例?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

说,我想创建一个内部有一些数据的单例。数据只能一次动态分配,就像在单身人士预期一样。



但是我现在想在何时以及如何发布这些数据。我应该建立一种破坏单身的特殊方法吗?更具体的 - 当这个单例的dealloc方法将被执行时?谁负责?

解决方案

您可以声明一个明确调用的方法/函数。



最简单的方法是让静态C ++类保存它,然后在析构函数中释放它。如果你有多个单身人士,那么这种方法并没有很好的扩展,因为破坏顺序是被定义的。



另一个替代方案(更好的设计)是避免单身方法,并将其作为在您的应用程序期间生活的另一个类的常规实例(应用程序代理是一个通常已知的示例)。



至于'何时,它取决于它的依赖性以及它的使用方式。尝试尽量减少外界对破坏的影响也是一件好事。


Say, I want to create a singleton which has some data inside. The data is dynamically allocated only once, as it expected on singleton.

But I would like to now under when and how this data can be is released. Should I establish special method which will destroy the singleton? To be more specific - when the method 'dealloc' for this singleton will be executed? who is responsible for that?

解决方案

You can declare a method/function you call explicitly.

The simplest way is to have a static C++ class hold it, then release it in its destructor. If you have multiple singletons, then this approach does not extend very well because the destruction order is implementation defined.

Another alternative (and better design) would be to avoid the singleton approach, and just use it as a regular instance in another class which lives for the duration of your app (an app delegate is a commonly known example).

As to 'when', it depends on its dependencies and how it's used. It's also good to try to minimize external influence in destruction.

这篇关于如何确保在IOS 5中使用ARC破坏单例?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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