Background Fetch
Fetching Small Amounts of Content Opportunistically
Apps that need to check for new content periodically can ask the system to wake them up so that they can initiate a fetch operation for that content. To support this mode, enable the Background fetch option from the Background modes section of the Capabilities tab in your Xcode project. (You can also enable this support by including the UIBackgroundModes key with the fetch value in your app’s Info.plist file.) Enabling this mode is not a guarantee that the system will give your app any time to perform background fetches. The system must balance your app’s need to fetch content with the needs of other apps and the system itself. After assessing that information, the system gives time to apps when there are good opportunities to do so.
When a good opportunity arises, the system wakes or launches your app into the background and calls the app delegate’s application:performFetchWithCompletionHandler: method. Use that method to check for new content and initiate a download operation if content is available. As soon as you finish downloading the new content, you must execute the provided completion handler block, passing a result that indicates whether content was available. Executing this block tells the system that it can move your app back to the suspended state and evaluate its power usage. Apps that download small amounts of content quickly, and accurately reflect when they had content available to download, are more likely to receive execution time in the future than apps that take a long time to download their content or that claim content was available but then do not download anything.
When downloading any content, it is recommended that you use the NSURLSession class to initiate and manage your downloads. For information about how to use this class to manage upload and download tasks, see URL Loading System Programming Guide.
在合適的時機更新數(shù)據(jù)
需要定期更新內(nèi)容的App可以要求系統(tǒng)在合適的時機喚醒它們,以便更新數(shù)據(jù)。要支持此功能,需要在Xcode項目的Capabilities選項卡的Background modes中啟用Background fetch選項(也可以通過在應(yīng)用程序的info.plist文件中對UIBackgroundModes字段中添加fetch)。啟用此功能并不能保證系統(tǒng)會在任何時間給喚起App并執(zhí)行后臺數(shù)據(jù)獲取。系統(tǒng)會平衡應(yīng)用各個App后臺獲取內(nèi)容的需求之后,會給出"合適時機"喚起對應(yīng)App并執(zhí)行后臺數(shù)據(jù)獲取。
當"合適時機"出現(xiàn)時,系統(tǒng)將后臺喚起App,并調(diào)用App的delegate回調(diào)application:performFetchWithCompletionHandler:。在該回調(diào)中檢查新內(nèi)容,并啟動下載操作。下載完新內(nèi)容后,必須執(zhí)行提供的completionHandler,并傳遞FetchResult結(jié)果,指示這次更新的內(nèi)容是否可用。completionHandler會通知系統(tǒng)可以將App切換至掛起狀態(tài)并評估其電源使用情況。快速的完成內(nèi)容下載并準確反映內(nèi)容的可用性的App,會比內(nèi)容下載耗時長或聲明需要內(nèi)容但不下載的App,在未來更有可能收到后臺喚起的機會。
下載任何內(nèi)容時,建議使用NSURLSession類管理下載。有關(guān)信息,請參閱URL Loading System Programming Guide。
Understanding When Your App Gets Launched into the Background
Apps that support background execution may be relaunched by the system to handle incoming events. If an app is terminated for any reason other than the user force quitting it, the system launches the app when one of the following events happens:
- For location apps:
- The system receives a location update that meets the app’s configured criteria for delivery.
- The device entered or exited a registered region. (Regions can be geographic regions or iBeacon regions.)
- For audio apps
- The audio framework needs the app to process some data. (Audio apps include those that play audio or use the microphone.)
- For Bluetooth apps:
- An app acting in the central role receives data from a connected peripheral.
- An app acting in the peripheral role receives commands from a connected central.
- For background download apps:
- A push notification arrives for an app and the payload of the notification contains the content-available key with a value of 1.
- The system wakes the app at opportunistic moments to begin downloading new content.
- For apps downloading content in the background using the NSURLSession class, all tasks associated with that session object either completed successfully or received an error.
- A download initiated by a Newsstand app finishes.
In most cases, the system does not relaunch apps after they are force quit by the user. One exception is location apps, which in iOS 8 and later are relaunched after being force quit by the user. In other cases, though, the user must launch the app explicitly or reboot the device before the app can be launched automatically into the background by the system. When password protection is enabled on the device, the system does not launch an app in the background before the user first unlocks the device.
后臺啟動App
App支持后臺啟動并處理一些事件,對于用戶主動Kill App,系統(tǒng)只會在以下事件發(fā)生時才會后臺啟動App:
- 定位相關(guān)App
- 系統(tǒng)接收到符合App配置信息的位置更新
- 設(shè)備進入或退出已指定區(qū)域(區(qū)域可以是地理區(qū)域或iBeacon區(qū)域)
- 音頻相關(guān)App
- audio framework需要應(yīng)用程序處理一些數(shù)據(jù)(音頻應(yīng)用程序包括播放音頻或使用麥克風)
- 藍牙相關(guān)App
- App通過藍牙發(fā)送數(shù)據(jù)
- App通過藍牙接收數(shù)據(jù)
- 支持后臺更新App
- aps消息體包含了
content-available: 1的鍵值對的靜默推送 - 系統(tǒng)會在“合適時機”喚醒App,開始下載新內(nèi)容
- 使用
NSURLSession在后臺下載內(nèi)容,并向系統(tǒng)反饋成功與失敗 - 由
Newsstand App觸發(fā)的數(shù)據(jù)下載
- aps消息體包含了
在大多數(shù)情況下,用戶強制退出應(yīng)用程序后,系統(tǒng)不會重新啟動應(yīng)用程序。一個例外是定位Apps,在iOS 8和更高版本中,它在用戶強制退出后會重新啟動。但在特定情況下,系統(tǒng)可以自動在后臺啟動,前提是用戶主動啟動過應(yīng)用程序或重新啟動設(shè)備。當設(shè)備上啟用密碼保護時,在用戶首次解鎖設(shè)備之前,系統(tǒng)不會在后臺啟動應(yīng)用程序。
總結(jié)
Background Fetch 具有下面幾個特性:
- 系統(tǒng)調(diào)度,對電量和數(shù)據(jù)的使用敏感
- 后臺啟動與正常啟動表現(xiàn)一致
- 與應(yīng)用實際的運行狀態(tài)無關(guān),可喚醒(進程從掛起到恢復(fù))或啟動(進程從無到有)
- 系統(tǒng)必須在30秒內(nèi)調(diào)用
completionHandler,否則進程將被殺死(performFetchWithCompletionHandler到completionHandler)
調(diào)度策略

使用方式
