didReceiveMemoryWarning and viewDidUnload memory question
I have 3 NSArrays of NSDictionarys that I populate if a button is selected (data is not created in viewDidLoad), do I get rid of this memory in b开发者_开发问答oth of these methods, or just one or the other? Thanks!
It depends on when you need this data.
- In
dealloc
, you should release all retained ivars. - In
viewDidUnload
, you should release any ivars that are recreated inviewDidLoad
.- For example, you might release an NSDictionary mapping ids to UIButtons, since these will be recreated in
viewDidLoad
anyway, but keep an NSDictionary that stores which ids are selected and which are not.
- For example, you might release an NSDictionary mapping ids to UIButtons, since these will be recreated in
- In
viewWillDisappear
orviewDidDisppear
, you can release any ivars holding data that should be reset whenever the user leaves and returns to the view. You may or may not want to do this conditionally depending on whether the user is leaving because of a modal or not. - In
didReceiveMemoryWarning
, you want to release any data that is being kept for faster access but can be reloaded from disk or recalculated.- For example, UIViewController will often keep its view around until it receives
didReceiveMemoryWarning
, even though the view is no longer visible.
- For example, UIViewController will often keep its view around until it receives
You'd use viewDidUnload
to release view objects. In this case, you'd want to release them in didReceiveMemoryWarning
, and if they are ivars (they probably are; otherwise you wouldn't have a reference to release them with), you should also release them in dealloc
.
精彩评论