后台获取枚举

Background Fetch Enumerations

在 iOS 中,后台提取的结果可能是以下之一:

  1. UIBackgroundFetchResultNewData
  2. UIBackgroundFetchResultNoData
  3. UIBackgroundFetchResultFailed

iOS在什么方面关心结果?

我知道持续时间过长(我相信 30 秒或更长时间)的抓取会因减少应用程序抓取机会而受到惩罚。

以上任何一项,特别是 NoDataFailed 是否也有影响? 或者这只是为了内部处理?

为什么不每次都 return UIBackgroundFetchResultNewData

没有描述 Apple 使用的精确算法,但在 iOS Application Programming Guide Apple 声明中,

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.

似乎 iOS 观察了您应用的行为。如果它声称有新内容可用(返回 UIBackgroundFetchResultNewData)但它实际上并未执行网络操作,它可能会收到不太频繁的后台获取机会。诚实是值得的。

我似乎还记得在某处读过(但现在找不到参考),iOS 可以使用完成值来确定一天中您的服务器可能有新内容的时间(例如,如果凌晨 1 点左右抓取 returns 新数据而下午 6 点抓取始终没有,iOS 可能更有可能在凌晨 1 点为您的应用程序执行后台抓取。

您的目标应该是尽快完成抓取,但在抓取完成之前不要调用完成处理程序,否则您的应用将在未完成下载的情况下暂停。您还可以使用 beginBackgroundTaskWithExpirationHandler 来获得更多时间,但是您的应用程序每次 'backgrounding' 的后台执行总时间限制为 180 秒(即,如果用户将您的应用程序带回前台,然后重新暂停 180 秒)。