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.


在合适的时机更新数据

需要定期更新内容的App可以要求系统在合适的时机唤醒它们,以便更新数据。要支持此功能,需要在Xcode项目的Capabilities选项卡的Background modes中启用Background fetch选项(也可以通过在应用程序的info.plist文件中对UIBackgroundModes字段中添加fetch)。启用此功能并不能保证系统会在任何时间给唤起App并执行后台数据获取。系统会平衡应用各个App后台获取内容的需求之后,会给出"合适时机"唤起对应App并执行后台数据获取。

当"合适时机"出现时,系统将后台唤起App,并调用App的delegate回调application:performFetchWithCompletionHandler:。在该回调中检查新内容,并启动下载操作。下载完新内容后,必须执行提供的completionHandler,并传递FetchResult结果,指示这次更新的内容是否可用。completionHandler会通知系统可以将App切换至挂起状态并评估其电源使用情况。快速的完成内容下载并准确反映内容的可用性的App,会比内容下载耗时长或声明需要内容但不下载的App,在未来更有可能收到后台唤起的机会。

下载任何内容时,建议使用NSURLSession类管理下载。有关信息,请参阅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,系统只会在以下事件发生时才会后台启动App:

  • 定位相关App
    • 系统接收到符合App配置信息的位置更新
    • 设备进入或退出已指定区域(区域可以是地理区域或iBeacon区域)
  • 音频相关App
    • audio framework需要应用程序处理一些数据(音频应用程序包括播放音频或使用麦克风)
  • 蓝牙相关App
    • App通过蓝牙发送数据
    • App通过蓝牙接收数据
  • 支持后台更新App
    • aps消息体包含了content-available: 1的键值对的静默推送
    • 系统会在“合适时机”唤醒App,开始下载新内容
    • 使用NSURLSession在后台下载内容,并向系统反馈成功与失败
    • Newsstand App触发的数据下载

在大多数情况下,用户强制退出应用程序后,系统不会重新启动应用程序。一个例外是定位Apps,在iOS 8和更高版本中,它在用户强制退出后会重新启动。但在特定情况下,系统可以自动在后台启动,前提是用户主动启动过应用程序或重新启动设备。当设备上启用密码保护时,在用户首次解锁设备之前,系统不会在后台启动应用程序。

总结

Background Fetch 具有下面几个特性:

  • 系统调度,对电量和数据的使用敏感
  • 后台启动与正常启动表现一致
  • 与应用实际的运行状态无关,可唤醒(进程从挂起到恢复)或启动(进程从无到有)
  • 系统必须在30秒内调用completionHandler,否则进程将被杀死(performFetchWithCompletionHandlercompletionHandler)
调度策略
使用方式

参考

©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 212,185评论 6 493
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 90,445评论 3 385
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 157,684评论 0 348
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 56,564评论 1 284
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 65,681评论 6 386
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 49,874评论 1 290
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 39,025评论 3 408
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 37,761评论 0 268
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 44,217评论 1 303
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 36,545评论 2 327
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 38,694评论 1 341
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 34,351评论 4 332
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 39,988评论 3 315
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 30,778评论 0 21
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 32,007评论 1 266
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 46,427评论 2 360
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 43,580评论 2 349

推荐阅读更多精彩内容

  • rljs by sennchi Timeline of History Part One The Cognitiv...
    sennchi阅读 7,312评论 0 10
  • **2014真题Directions:Read the following text. Choose the be...
    又是夜半惊坐起阅读 9,437评论 0 23
  • 在生活中,我们经常会看到这样的现象,有的人在社会上遇到挫折以后,就变得比较多愁善感,对生活比较消极,有时候,没事的...
    爱思考的90后阅读 1,561评论 0 2
  • 小组两个储备主任,有一个升上去当主任了,一人一个十几人的团队少了一个人带,领导就选中了我。 不知道是好事还是坏事,...
    翻滚吧海阔天空阅读 125评论 0 1
  • Quarter life crisis (四分之一人生危机)听起来挺扯淡的,今年却莫名其妙地感同身受了。二十岁的前...
    王湿人阅读 752评论 0 1