0

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?

JosephH
  • 37,173
  • 19
  • 130
  • 154
  • Are you sure it "no longer saves any significant amount of memory" even on iOS5? Presumably optimizations in iOS6 made the old mechanism obsolete. – Thilo Oct 17 '12 at 11:45
  • 1
    Putting a retain in viewDidLoad and not having a release before dealloc would mean dealloc never gets called, so no, that's not the way you want to handle this. – Filip Radelic Oct 17 '12 at 11:47
  • @FilipRadelic I was suggesting a retain on self.view - that won't stop self being dealloced as far as I know, though may be a bad idea for other reasons. – JosephH Oct 17 '12 at 12:00
  • @Thilo no, I'm definitely not sure about that. The WWDC video seems to imply that unloaded the views hasn't made any noticeable difference for "some time". I guess I would need to do some measurements to be sure. – JosephH Oct 17 '12 at 12:50

2 Answers2

1

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.

Michael Ochs
  • 2,846
  • 3
  • 27
  • 33
  • That's wrong - it has been possible in iOS 5, by just overriding didReceiveMemoryWarning. – Eiko Oct 17 '12 at 11:50
  • @madboy Thanks! I would be very interested to know for sure if the backing stores will be released on iOS5 or not, if you know of any definitive reference. – JosephH Oct 17 '12 at 12:52
  • @Eiko I corrected my statement above: It is not supported to do so. Overriding didReceiveMemoryWarning might have other side effects, as you don't know, what a UIViewController does in didReceiveMemoryWarning! – Michael Ochs Oct 17 '12 at 13:13
  • @JosephH Sorry, I don't know this for sure, but you might want to set a break point in didReceiveMemoryWarning and see what iOS 5 does in this call. – Michael Ochs Oct 17 '12 at 13:15
1

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.

Eiko
  • 25,601
  • 15
  • 56
  • 71