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.

Responding to low-memory warnings<br />

197<br />

original view in memory and simply reuses it when the user returns to it (you could<br />

handle the viewDidAppear message if you needed to perform an action each time the<br />

view becomes visible to the user).<br />

By comparison, if you switch to the second tab and then simulate a low-memory situation,<br />

the debugger should break in FirstViewController’s viewDidUnload method,<br />

indicating the view controller has decided it can help resolve the low-memory situation<br />

by deallocating the view that’s currently invisible to the user.<br />

Once the root view has been unloaded, you can continue using the application<br />

with no obvious difference in behavior. The next time you return to the first tab, you<br />

should notice that your viewDidLoad implementation is automatically invoked in<br />

order to reinitialize the view that the framework has just reloaded from your NIB file.<br />

Knowing that a view can suddenly and perhaps unexpectedly become deallocated,<br />

you should be careful not to store important application state in the view. Apart from<br />

breaking the model-view-controller design pattern principles, it won’t work reliably. As<br />

an example, if you use the enabled state of a UIButton or a selected item in a<br />

UITableView to control the logic or behavior of your application, you may notice your<br />

application enters an unexpected state when the view becomes deallocated and then<br />

re-created, causing all of the controls to return to the state in which they were<br />

declared in the NIB file.<br />

In general, the correct place for view initialization logic is in the viewDidLoad<br />

method. This logic is invoked whenever the view associated with the view controller is<br />

constructed. If you take a look at RootViewController’s viewDidLoad method implementation<br />

within the Rental Manager application, you’ll notice this is exactly where<br />

the cityMappings dictionary is created, ensuring it’s also re-created if it’s destroyed<br />

due to a low-memory situation.<br />

As an extension to this rule of thumb, you may also like to override the matching<br />

viewDidUnload message as an ideal place to save application state just before the view<br />

is removed from memory.<br />

9.6.3 Observing the<br />

UIApplicationDidReceiveMemoryWarningNotification<br />

Sometimes when writing a support class deep in your application logic, it may be<br />

inconvenient for a view controller or application delegate to be responsible for freeing<br />

any associated resources in a low-memory situation. For one thing, doing so would<br />

break encapsulation, as something external to the class would require knowledge of<br />

and access to its internals in order to free them. For such situations, Cocoa Touch provides<br />

an alternative solution via a feature called notifications.<br />

A notification allows a class to respond to an event, such as a low-memory situation,<br />

no matter where the class is positioned in the application; it no longer needs to be a<br />

UIViewController subclass. In general, an object called the sender will generate a<br />

notification and dispatch it to be received by zero or more objects called observers.<br />

The glue that connects notification senders with observers is a class called<br />

NSNotificationCenter. This class provides a mechanism for senders to broadcast

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

Saved successfully!

Ooh no, something went wrong!