简体   繁体   English

保留Appdelegate

[英]Retaining Appdelegate

I know that there is no need to retain a delegate, so as to avoid retain cycles. 我知道没有必要保留代表,以避免保留周期。 I happened to meet the question in an interview, "What happens if appDelegate is retained?". 我在一次采访中碰巧遇到了一个问题:“如果保留appDelegate,会发生什么?”。 I have no answer for this and seeking an answer here for my knowledge. 我对此没有答案,并据我所知在这里寻求答案。 Thanks 谢谢

The application delegate typically is created once and exists during the entire lifetime of the application and is never deallocated. 应用程序委托通常创建一次,并且在应用程序的整个生命周期中都存在,并且永远不会被释放。 Therefore retaining it will have no negative consequences. 因此保留它不会有负面影响。

The question is probably supposed to be concerning a custom "delegate" as in the case below: 这个问题可能与一个自定义的“委托”有关,如下所示:

@property (nonatomic,weak) id delegate;

The reason that objects weakly retain their delegates is to avoid retain cycles. 对象弱保留其委托的原因是为了避免保留周期。 Imagine the following scenario: object a creates b and retains it, then sets itself as b's delegate. 想象以下情形:对象a创建b并保留它,然后将其自身设置为b的委托。 a is released by its owner, leaving a retain cycle containing a and b. a由其所有者释放,留下包含a和b的保留周期。 See the apple documentation on retain cycles . 有关保留周期,请参阅Apple文档。

If that is not the case. 如果不是这样的话。 Concerning the AppDelegate Like any other object, the app delegate will be deallocated when no other object has retained it. 关于AppDelegate像任何其他对象一样,当没有其他对象保留应用程序委托时,该应用程序委托将被释放。 It's pretty unusual to have an app delegate that doesn't stick around until the app terminates , The app may not bother to release and deallocate anything just before it exits. 直到应用程序终止时才会有一个应用程序委托,这是很不寻常的 ,该应用程序可能不会费心地在退出之前释放和重新分配任何东西。

In short if you retain the AppDelegate it might not be released ... 简而言之,如果您保留AppDelegate,则它可能不会被释放...

You might find this usefull 您可能会发现有用

保留appDelegate根本无关紧要,因为不应在应用程序退出之前取消分配此对象。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM