Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UIViewController prevent view from unloading

When my iPhone app receives a memory warning the views of UIViewControllers that are not currently visible get unloaded. In one particular controller unloading the view and the outlets is rather fatal.

I'm looking for a way to prevent this view from being unloaded. I find this behavior rather stupid - I have a cache mechanism, so when a memory warning comes - I unload myself tons of data and I free enough memory, but I definitely need this view untouched.

I see UIViewController has a method unloadViewIfReloadable, which gets called when the memory warning comes. Does anybody know how to tell Cocoa Touch that my view is not reloadable?

Any other suggestions how to prevent my view from being unloaded on memory warning?

Thanks in advance


Apple docs about the view life cycle of a view controller says:

didReceiveMemoryWarning - The default implementation releases the view only if it determines that it is safe to do so

Now ... I override the didReceiveMemoryWarning with an empty function which just calls NSLog to let me know a warning was received. However - the view gets unloaded anyway. Plus, on what criteria exactly is decided whether a view is safe to unload ... oh ! so many questions!

like image 360
Marin Todorov Avatar asked Jun 03 '10 11:06

Marin Todorov


1 Answers

According to the docs, the default implementation of didReceiveMemoryWarning: releases the view if it is safe to do (ie: superview==nil).

To prevent the view from being released you could override didReceiveMemoryWarning: but in your implementation do not call [super didReceiveMemoryWarning]. That's where the view is released by default (if not visible).

The default didReceiveMemoryWarning releases the view by calling [viewcontroller setView:nil], so you could override that instead.

like image 183
progrmr Avatar answered Oct 12 '22 08:10

progrmr