Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p><strong>The short answer:</strong></p> <p>Never use -didReceiveMemoryWarning for a balanced tear down as it might get called never or multiple times. If you have your setup in -viewDidLoad, place your cleanup code in -dealloc.</p> <hr> <p><strong>The long answer:</strong></p> <p>It's not easy to give a general answer, as it really depends on the situation. However, there are two important facts to state:</p> <p><strong>1. -viewDidUnload is deprecated and in fact never called starting with iOS6 and later. So, if you have your cleanup code in there, your app leaks under these OS versions</strong></p> <p><strong>2. -didReceiveMemoryWarning might be called multiple times or never. So it's a really bad place for a balanced teardown of objects you created somewhere else</strong></p> <p>My answer is looking at the common case where you are using properties, for example:</p> <pre><code>@property (strong) UIView *myCustomView // &lt;-- this is what I'm talking about @property (assign) id *myDelegate </code></pre> <p>Here you have to do some cleanup, because you either created and own the customView or InterfaceBuilder created it, but you are retaining it. Before iOS 6 you would probably have done something like this:</p> <pre><code>- (void)viewDidLoad { self.myCustomView = [[UIView alloc] initWithFrame:…]; } - (void)viewDidUnload { // &lt;-- deprecated! [myCustomView removeFromSuperView]; self.myCustomView = nil; } </code></pre> <p>...because (again) <code>myCustomView</code> is a retained property, created and owned by you and so you have to take care and "release" it (set it to nil) at the end.</p> <p>With iOS 6, the best place to replace <code>-viewDidUnload</code> and to set the retained property to nil is probably <code>-dealloc</code>. There's also <code>viewWillAppear</code> and <code>viewDidDisappear</code>, but these are not tied to the lifecycle of your view/controller, but the display cycle (On the other hand, the -...appear methods are perfect for un-/registering notification listeners!). So it might not be appropriate to create and destroy views before and after every display. <code>dealloc</code> is the only method we can be sure of to be called at the very end of the controller's lifecycle. Note that you must not call <code>[super dealloc]</code> if you're using ARC:</p> <pre><code>- (void)dealloc { self.myCustomView = nil; } </code></pre> <p>However, if you're using <code>viewDidLoad</code> to do some view related setup that can be freed upon low memory conditions, the other posts showing how to deal with low memory situations are totally valid. In this case you can also use dealloc, but you have to check if your views are still there.</p> <h2>The Lifecycle of a ViewController</h2> <p>Maybe it's also helpful to look a the general lifecycle of a ViewController:</p> <p>This is the lifetime of a viewController <em>(lines in italic mean that these methods might be called multiple times)</em>:</p> <blockquote> <ul> <li>init: ViewController loaded, no interface element (IBOutlet) available yet (all nil)</li> <li>viewDidLoad: the nib/storyboard has been loaded and all objects are available. The user sees nothing yet</li> <li><em>viewWillAppear: the view is about to be displayed</em></li> <li><em>viewDidAppear: the view is on screen</em></li> <li><em>viewWillDisappear: the view is about to go away</em></li> <li><em>viewDidDisappear: the view just was taken off the window</em></li> <li>viewDidUnload: NEVER CALLED in iOS6/7</li> <li><em>didReceiveMemoryWarning: You don’t know if, when and how often this is called. Prior to iOS6 it might unload the view, after iOS6 it just purges an offscreen cache or does nothing</em></li> <li>dealloc: the viewController is about to get destroyed</li> </ul> </blockquote> <p>So, to sum it up there are various possibilities; what goes where now really depends on what was initialized where:</p> <ul> <li>-dealloc if created in -init: or -viewDidLoad:</li> <li>-viewWill/DidDisappear (paired with -viewWill/DidAppear)</li> <li>-didReceiveMemoryWarning (might or might not be called)</li> </ul>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload