Question

Given that iOS6 no longer unloads views, and hence no longer calls UIViewController's viewDidUnload, I was wondering if there is a reliable way to cause this same behaviour on iOS5?

That is, I'd really like to stop my views unloading on iOS5, for all the same reasons Apple give for why they no longer unload views on iOS 6 (because it no longer saves any significant amount of memory, and is almost always a source of extra bugs). I'd far rather spend my time developing useful features than testing lots of cases that can now only happen on iOS5 and only in low memory!

I've had a search around and can't find anyone who has asked this question before, which surprises me. Could it just be as simple as retaining an extra reference to self.view in viewDidLoad (and releasing it only in dealloc)? Are there any likely traps?

Was it helpful?

Solution

On iOS5 it is not supported to stop views from unloading and this would also result in a huge memory problem as the way iOS6 handles views is completely different. In iOS6 the backing store of the views is still be removed from memory if necessary - or at least it is marked to be able to be removed from memory. To my knowledge, on iOS5 this isn't the case. The backing store is removed only if the view itself is removed, even if the view itself is only a couple of bytes in size.

Have a look at this great post: View Controller Lifecycle in iOS 6 This might give you an idea on how complex is, what you are asking for.

OTHER TIPS

The main difference now is that views are not automatically unloaded in didReceiveMemoryWarning, and viewDidUnload isn't called.

This doesn't mean that you shouldn't unload your views - in fact, in WWDC videos they suggest to do so in didReceiveMemoryWarning. The main difference now is that you are absolutely in control what happens and when. Many people relied on code in viewDidUnload which often wasn't called as expected anyway - for example when just removing the view and releasing the controller.

If you don't want to release your view on memory warnings (which - again - might or might not be what you want), you can just don't set the view property to nil. If you have an empty didReceiveMemoryWarning in your controller, that will work in iOS 5 as well (without calling on super).

I seriously advice to take those warnings serious, though. They are your only point to clean up with possibly unused memory, and backing stores of views can get really big.

Edit: You should only consider not unloading your views if there are few of them, frequently used and with low memory footprint.

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