简体   繁体   中英

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?". 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 is released by its owner, leaving a retain cycle containing a and b. See the apple documentation on retain cycles .

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. 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 ...

You might find this usefull

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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