iOS - viewDidUnload not called, but didReceiveMemoryWarning is called

2.5k Views Asked by At

Trying to hunt down the cause of crashing on certain devices. I am noticing that my view controllers are receiving didReceiveMemoryWarning, but NOT viewDidUnload. And according to Apple:

you would not use didReceiveMemoryWarning to release references to view objects, you might use it to release any view-related data structures that you did not already release in your viewDidUnload method. (The view objects themselves should always be released in the viewDidUnload method.)

So,

A: Why is viewDidUnload not called? I can't remove my view objects here if it is never called.

B: If I'm not supposed to remove my view objects in didReceiveMemoryWarning, where else would I do this?

C: Using ARC, should I still need to remove view objects, set arrays to nil, etc?

3

There are 3 best solutions below

0
On

As the other mentioned viewDidUnload: is deprecated in iOS 6. But as additional information you should know, that it is seldom necessary to unload a UIView since iOS 6 is doing its magic thingie in the background -it is destroying the bitmap layer of the backing CALayer of the view (which is by far the biggest "part" of a UIView). If the view is needed again iOS will call drawRect: where you compose your view and everything will be ok.

For more information read this great article of Joe Conway: ViewController lifecycle in iOS 6

0
On

didReceiveMemoryWarning is specifically targeted not to the unloading of a view, but rather for the view controller to release objects which can easily be recreated (i.e. UIIamges and the like). You should not release objects in your view unless they can easily be recreated as necessary.

3
On

viewDidUnload is deprecated in iOS6. You "can" remove views in didReceiveMemoryWarning if you think it is necessary, but it is left up to you.

This thread may help as well.

viewDidUnload no longer called in ios6