01.02.2014 Views

Objective-C Fundamentals

Objective-C Fundamentals

Objective-C Fundamentals

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

194 CHAPTER 9 Memory management<br />

Figure 9.3 The Simulate Memory<br />

Warning option found in the Hardware<br />

menu of the iPhone Simulator can be<br />

used to test how well your application<br />

responds to low-memory scenarios.<br />

For reasons outlined previously, the iPhone Simulator isn’t a representative example<br />

of the kinds of memory constraints your applications will find themselves in while running<br />

on a real device. To help developers test their applications in low-memory conditions,<br />

the iPhone Simulator provides a way to generate a simulated low-memory<br />

situation. This feature can be initiated by selecting Simulate Memory Warning in the<br />

Hardware menu (see figure 9.3). The simulator fakes a low-memory situation, no matter<br />

how much memory is currently available on your computer.<br />

When you select the Simulate Memory Warning option, you should see that your<br />

application delegate is sent an applicationDidReceiveMemoryWarning: message, as<br />

demonstrated by the following log messages appearing in the Xcode debugger console:<br />

Received simulated memory warning.<br />

Hello, we are in a low-memory situation!<br />

Notice that the iPhone Simulator also emits a log message stating that the low-memory<br />

situation was faked. Obviously, if a real low-memory situation occurs on a device, you<br />

wouldn’t see this additional log message.<br />

In a typical application it’s possible that your UIApplicationDelegate implementation<br />

may not have easy access to significant resources that it can free on a moment’s<br />

notice. It’s more probable that the individual UIViews and UIViewControllers that<br />

make up the visual aspects of your application will have access to the memory<br />

resources that can be returned to the operating system. Therefore, the UIView-<br />

Controller class provides an alternative mechanism to access low-memory warnings<br />

in the form of a method called didReceiveMemoryWarning that can be overridden by<br />

interested subclasses.<br />

9.6.2 Overriding didReceiveMemoryWarning<br />

In a UIViewController subclass, such as the RootViewController class in the Rental<br />

Manager application, you can handle the low-memory warning condition by implementing<br />

a method named didReceiveMemoryWarning:<br />

- (void)didReceiveMemoryWarning {<br />

[super didReceiveMemoryWarning];<br />

[cityMappings release];

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!