01.02.2014 Views

Objective-C Fundamentals

Objective-C Fundamentals

Objective-C Fundamentals

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Responding to low-memory warnings<br />

193<br />

- (NSString *)initWithString:(NSString *)str;<br />

+ (NSString *)stringWithString:(NSString *)str;<br />

The first line initializes a string you’ve explicitly allocated with a call to alloc. The second<br />

performs a similar task, but it doesn’t need a string to be allocated beforehand.<br />

Internally this method allocates a new string object and initializes it as required before<br />

sending it an autorelease message and returning it.<br />

9.6 Responding to low-memory warnings<br />

The iPhone operating system implements a cooperative approach when it comes to<br />

memory management and provides a warning to each application when the device<br />

finds itself in a low-memory situation.<br />

If the amount of free memory drops below a threshold, the operating system<br />

attempts to release any excess memory that it holds, and if this doesn’t free up enough<br />

memory, it also sends currently running applications a warning. If your application<br />

receives a low-memory warning, it’s your chance to free up as much memory as possible<br />

by deallocating unneeded objects or by clearing out cached information that can<br />

easily be regenerated when next required.<br />

The UIKit application framework provides access to the low-memory warning via a<br />

number of mechanisms:<br />

■<br />

■<br />

■<br />

Implementing the applicationDidReceiveMemoryWarning: message in your<br />

application delegate<br />

Overriding the didReceiveMemoryWarning message in your UIViewController<br />

subclass<br />

Registering for the UIApplicationDidReceiveMemoryWarningNotification<br />

notification<br />

We cover each of these scenarios in turn. It’s important that your application responds<br />

to these warnings. If your application doesn’t respond, or you don’t manage to free<br />

enough memory, the operating system may take even more drastic measures such<br />

as “randomly” and abruptly exiting your application in its effort to gain additional<br />

free memory.<br />

9.6.1 Implementing the UIApplicationDelegate protocol<br />

As discussed in chapter 7, protocols can have optional messages, which you can selectively<br />

decide to implement. The UIApplicationDelegate protocol (implemented by<br />

the RentalManagerAppDelegate class in the Rental Manager application) has an<br />

optional message called applicationDidReceiveMemoryWarning:, which is sent whenever<br />

the application detects a low-memory situation. This message can be implemented<br />

as follows:<br />

- (void)applicationDidReceiveMemoryWarning:(UIApplication *)application<br />

{<br />

NSLog(@"Hello, we are in a low-memory situation!");<br />

}

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

Saved successfully!

Ooh no, something went wrong!