Given that we are writing code with ARC, should I nil properties in viewDidUnload, that are instantiated from:
-
XIB (here the nilling is sometimes generated from IDE)
-
from initialiser and have not IBOutlet
- that are weak
?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
The purpose of
viewDidUnloadis to give your app a chance to remove references to user interface objects that may no longer exist because the view was removed upon receiving a memory warning. Thus:You should be setting any user interface controls to
nil(because the view is being unloaded). Unnecessary forweakproperties (and they generally should beweakper Apple guidance on this topic), but if Xcode inserts it in for you, it’s not a problem. For guidance on what to do inviewDidUnloadsee the “Memory Warnings” section of the Resource Programming Guide: The Nib Files.And for non-user interface objects that you set up in
viewDidLoad, I’d be wary about just blindly setting those tonilinviewDidUnload, especially if you’re not using ARC (e.g. if you accidentallynilthe instance variable, you might cause a leak). And you probably want to balance the amount of memory that will be recovered versus the “cost” of re-retrieving that data (e.g. if it’s from some remote server). Regardless, I’d suggest that you handle the freeing of memory for non-UI objects indidReceiveMemoryWarning.In my mind, I view
viewDidUnloadas a chance to make sure I’m not maintaining references to user interface objects that might no longer exist, and I usedidReceiveMemoryWarningto empty caches or other non-UI related items that I can safely purge to recover some memory. And if you’re concerned about iOS 6, note that the handling ofviewDidUnloadmay be changing, and while the NDA precludes us from discussing it publicly, I might advise that you may want to refer to the iOS 6 Beta 4 Release Notes and look for references toviewDidUnload.