views:

112

answers:

1

Hi,

Here is a piece of code from the exploring the iPhone SDK book. It uses the example of 2 views. It checks to see which view is being used and will release the other one.

- (void)didReceiveMemoryWarning {

    [super didReceiveMemoryWarning]; // Releases the view if it
    // doesn't have a superview

    // Release anything that's not essential, such as cached data
    if (self.blueViewController.view.superview == nil)
        self.blueViewController = nil;
    else
       self.yellowViewController = nil;
}

My question is: If my app has many views e.g 15, would I need to check each view as above and release what's not being used? Or is there a different technique used in that case?`

A: 

The concept is what is more important from the book (releasing anything nonessential in the case of a low memory warning) rather than the details of the example (purging the view you aren't using).

In the case of the many views your application is using my first question would be how often are the views being switched? If the user is staying on one view or another for a while it might be best to free up the other views as you transition them off the screen to preemptively give your app as much free memory as possible. In this case the book's example really wouldn't apply as you'd already have all the 'cached' views purged. If they are quickly switching between views then the off-screen views are good to keep around (setting up a new view is somewhat expensive.) In this case you'll need to follow the book's example in a low memory event.

fbrereto
Thanks very much,you've helped clear so much up. So if I know the user will be on one view for a while as soon as that view loads, I should release the previous view from memory?
david
That would be the route I would take.
fbrereto