Do not rely viewDidUnload
to do any cleanup. That's only called in iOS versions prior to iOS 6, and only when the view is unloaded due to memory pressure (but not when you just dismiss/pop the view).
Set your delegate to nil
in the dealloc
method or viewDidDisappear
or wherever is appropriate.
Two caveats relevant to picking which method you'll nil
the delegate
:
Be aware that viewWillDisappear
and viewDidDisappear
will also be called if you push/present another view controller, even if the current one has not been yet been dismissed. Only rely upon these disappear-related methods if the view controller in question does not ever push/present another view controller.
If employing the dealloc
technique, note that this only works if the delegate
is a weak
property of the image fetcher class (and delegates generally should be weak
). If the delegate
was a strong
or retain
property, that will prevent the view controller's dealloc
from ever getting called.
By the way, I gather that you are letting the image fetch continue, even though the view controller has been dismissed. You might want to not only nil
the delegate, but cancel the request, too. It depends upon whether (a) you're using a fetch that even permits a cancellation (e.g. a NSURLConnectionDataDelegate
approach or a AFNetworking operation) and, if so, (b) whether you want it to cancel or not. It's easy, though, to tie up precious network resources (esp if on a slow cellular connection) letting requests continue even if the user doesn't need it anymore. It depends upon the particulars of your app.
Regardless, do not rely upon viewDidUnload
.