Question

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

Was it helpful?

Solution

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.

OTHER TIPS

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

Retaining the appDelegate does not matter at all since this object is not supposed to be deallocated before the app quits.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top