Background Fetch

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ī)更新數(shù)據(jù)

需要定期更新內(nèi)容的App可以要求系統(tǒng)在合適的時(shí)機(jī)喚醒它們,以便更新數(shù)據(jù)。要支持此功能,需要在Xcode項(xiàng)目的Capabilities選項(xiàng)卡的Background modes中啟用Background fetch選項(xiàng)(也可以通過在應(yīng)用程序的info.plist文件中對(duì)UIBackgroundModes字段中添加fetch)。啟用此功能并不能保證系統(tǒng)會(huì)在任何時(shí)間給喚起App并執(zhí)行后臺(tái)數(shù)據(jù)獲取。系統(tǒng)會(huì)平衡應(yīng)用各個(gè)App后臺(tái)獲取內(nèi)容的需求之后,會(huì)給出"合適時(shí)機(jī)"喚起對(duì)應(yīng)App并執(zhí)行后臺(tái)數(shù)據(jù)獲取。

當(dāng)"合適時(shí)機(jī)"出現(xiàn)時(shí),系統(tǒng)將后臺(tái)喚起App,并調(diào)用App的delegate回調(diào)application:performFetchWithCompletionHandler:。在該回調(diào)中檢查新內(nèi)容,并啟動(dòng)下載操作。下載完新內(nèi)容后,必須執(zhí)行提供的completionHandler,并傳遞FetchResult結(jié)果,指示這次更新的內(nèi)容是否可用。completionHandler會(huì)通知系統(tǒng)可以將App切換至掛起狀態(tài)并評(píng)估其電源使用情況??焖俚耐瓿蓛?nèi)容下載并準(zhǔn)確反映內(nèi)容的可用性的App,會(huì)比內(nèi)容下載耗時(shí)長或聲明需要內(nèi)容但不下載的App,在未來更有可能收到后臺(tái)喚起的機(jī)會(huì)。

下載任何內(nèi)容時(shí),建議使用NSURLSession類管理下載。有關(guān)信息,請(qǐng)參閱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.


后臺(tái)啟動(dòng)App

App支持后臺(tái)啟動(dòng)并處理一些事件,對(duì)于用戶主動(dòng)Kill App,系統(tǒng)只會(huì)在以下事件發(fā)生時(shí)才會(huì)后臺(tái)啟動(dòng)App:

  • 定位相關(guān)App
    • 系統(tǒng)接收到符合App配置信息的位置更新
    • 設(shè)備進(jìn)入或退出已指定區(qū)域(區(qū)域可以是地理區(qū)域或iBeacon區(qū)域)
  • 音頻相關(guān)App
    • audio framework需要應(yīng)用程序處理一些數(shù)據(jù)(音頻應(yīng)用程序包括播放音頻或使用麥克風(fēng))
  • 藍(lán)牙相關(guān)App
    • App通過藍(lán)牙發(fā)送數(shù)據(jù)
    • App通過藍(lán)牙接收數(shù)據(jù)
  • 支持后臺(tái)更新App
    • aps消息體包含了content-available: 1的鍵值對(duì)的靜默推送
    • 系統(tǒng)會(huì)在“合適時(shí)機(jī)”喚醒App,開始下載新內(nèi)容
    • 使用NSURLSession在后臺(tái)下載內(nèi)容,并向系統(tǒng)反饋成功與失敗
    • Newsstand App觸發(fā)的數(shù)據(jù)下載

在大多數(shù)情況下,用戶強(qiáng)制退出應(yīng)用程序后,系統(tǒng)不會(huì)重新啟動(dòng)應(yīng)用程序。一個(gè)例外是定位Apps,在iOS 8和更高版本中,它在用戶強(qiáng)制退出后會(huì)重新啟動(dòng)。但在特定情況下,系統(tǒng)可以自動(dòng)在后臺(tái)啟動(dòng),前提是用戶主動(dòng)啟動(dòng)過應(yīng)用程序或重新啟動(dòng)設(shè)備。當(dāng)設(shè)備上啟用密碼保護(hù)時(shí),在用戶首次解鎖設(shè)備之前,系統(tǒng)不會(huì)在后臺(tái)啟動(dòng)應(yīng)用程序。

總結(jié)

Background Fetch 具有下面幾個(gè)特性:

  • 系統(tǒng)調(diào)度,對(duì)電量和數(shù)據(jù)的使用敏感
  • 后臺(tái)啟動(dòng)與正常啟動(dòng)表現(xiàn)一致
  • 與應(yīng)用實(shí)際的運(yùn)行狀態(tài)無關(guān),可喚醒(進(jìn)程從掛起到恢復(fù))或啟動(dòng)(進(jìn)程從無到有)
  • 系統(tǒng)必須在30秒內(nèi)調(diào)用completionHandler,否則進(jìn)程將被殺死(performFetchWithCompletionHandlercompletionHandler)
調(diào)度策略
使用方式

參考

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • rljs by sennchi Timeline of History Part One The Cognitiv...
    sennchi閱讀 7,817評(píng)論 0 10
  • **2014真題Directions:Read the following text. Choose the be...
    又是夜半驚坐起閱讀 11,005評(píng)論 0 23
  • 在生活中,我們經(jīng)常會(huì)看到這樣的現(xiàn)象,有的人在社會(huì)上遇到挫折以后,就變得比較多愁善感,對(duì)生活比較消極,有時(shí)候,沒事的...
    愛思考的90后閱讀 1,699評(píng)論 0 2
  • 小組兩個(gè)儲(chǔ)備主任,有一個(gè)升上去當(dāng)主任了,一人一個(gè)十幾人的團(tuán)隊(duì)少了一個(gè)人帶,領(lǐng)導(dǎo)就選中了我。 不知道是好事還是壞事,...
    翻滾吧海闊天空閱讀 180評(píng)論 0 1
  • Quarter life crisis (四分之一人生危機(jī))聽起來挺扯淡的,今年卻莫名其妙地感同身受了。二十歲的前...
    王濕人閱讀 860評(píng)論 0 1

友情鏈接更多精彩內(nèi)容