Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Beginning iOS5 Development.pdf
Скачиваний:
7
Добавлен:
09.05.2015
Размер:
15.6 Mб
Скачать

546

CHAPTER 15: Grand Central Dispatch, Background Processing, and You

device displays the system alert showing the incoming message, this will appear in the Xcode console:

2011-10-31 12:05:15.391 State Lab[1069:307] applicationWillResignActive:

Note that our app didn’t get sent to the background. It’s in the Inactive state and can still be seen behind the system alert. If this app were a game or had any video, audio, or animations running, this is where we would probably want to pause them.

Press the Close button on the alert, and you’ll get this:

2011-10-31 12:05:24.808 State Lab[1069:307] applicationDidBecomeActive:

Now let’s see what happens if you decide to reply to the message instead. Have someone send you another message, generating this:

2011-10-31 12:11:04.154 State Lab[1069:307] applicationWillResignActive:

This time hit Reply, which switches you over to the Messages app, and you should see the following flurry of activity:

2011-10-31

12:11:07.826 State Lab[1069:307] applicationDidBecomeActive:

2011-10-31

12:11:07.966

State

Lab[1069:307]

applicationWillResignActive:

2011-10-31

12:11:07.984

State

Lab[1069:307]

applicationDidEnterBackground:

Interesting! Our app quickly becomes Active again, then becomes Inactive again, and finally goes to Background (and then, silently, Suspended).

Making Use of Execution State Changes

So, what should we make of all this? Based on what we’ve just demonstrated, it seems like there’s a clear strategy to follow when dealing with these state changes:

Active Inactive

Use applicationWillResignActive:/UIApplicationWillResignActiveNotification to “pause” your app’s display. If your app is a game, you probably already have the ability to pause the gameplay in some way. For other kinds of apps, make sure no time-critical demands for user input are in the works, because your app won’t be getting any user input for a while.

Inactive Background

Use applicationDidEnterBackground:/UIApplicationDidEnterBackgroundNotification to release any resources that don’t need to be kept around when the app is backgrounded (such as cached images or other easily reloadable data) or that wouldn’t survive backgrounding anyway (such as active network connections). Getting rid of excess memory usage here will make your app’s eventual Suspended snapshot smaller, thereby decreasing the risk that your app will be purged from RAM entirely. You should also use this opportunity to save any application data that will help your users pick up where they left off the next time your app is relaunched. If your app comes back to the

www.it-ebooks.info

Соседние файлы в предмете [НЕСОРТИРОВАННОЕ]