Apple 允许后台任务执行多长时间 运行?
How long does Apple permit a background task to run?
我必须将一组图像文件上传到数据库,因此,我偶然发现 Apple's background execution guide 以确保当用户暂停或终止我的应用程序时应用程序仍会上传数据。
但是在描述中,如果我们调用beginBackgroundTaskWithName:expirationHandler:
或beginBackgroundTaskWithExpirationHandler:
来启动后台任务,它会说giving it a little extra time to finish its work
。
little extra time
到底有多长?
时间量会因许多不同的变量而有所不同,但可以参考 UIApplication
:
上的 backgroundTimeRemaining
属性 检查该值
NSLog(@"Time Remaining: %f", [[UIApplication sharedApplication] backgroundTimeRemaining]);
理论上,你有 2/3 分钟的时间关闭你想在后台执行的任务,如果你不这样做,你的应用程序可能会被杀死。
之后,您可以调用“beginBackgroundTaskWithExpirationHandler”,您必须做好准备,以防万一 Apple 提供的 'little extra time' 不足以完成您需要完成的任务。
编辑:
When an iOS application goes to the background, are lengthy tasks paused?:
Return from applicationDidEnterBackground(_:)
as quickly as possible. Your implementation of this method has approximately five seconds to perform any tasks and return. If the method doesn’t return before time runs out, your app is terminated and purged from memory.
来自雷温德利希:
'Again, there are no guarantees and the API documentation doesn’t even give a ballpark number – so don’t rely on this number. You might get 5 minutes or 5 seconds, so your app needs to be prepared for anything!':
http://www.raywenderlich.com/29948/backgrounding-for-ios
您的应用进入后台后的等待时间由 iOS 决定。无法保证您获得的时间,但您可以随时查看 UIApplication 的 backgroundTimeRemaining 属性。这将告诉您还剩多少时间。
一般的、基于观察的共识是,通常你有 10 分钟的时间。同样,没有任何保证,API 文档甚至没有给出大概数字——所以不要依赖这个数字。您可能需要 5 分钟或 5 秒,因此您的应用需要为任何事情做好准备!
如果你想在应用程序处于后台时上传你的文件,你应该使用苹果的后台服务。 iOS 将为您的应用程序提供大约的时间。 3 分钟(根据一些经验)完成你的任务,然后它会杀死你的应用程序。
Apple 允许在特殊情况下使用更长的 运行 应用程序。为此,您需要在 info.plist 文件中使用 UIBackgroundModes。有关这些特殊情况的更多信息,请参阅 table 3-1 on this link.
Here 是一篇很好的文章,描述了后台任务 运行 时间以及如何在 iOS 中实现长时间的 运行ning 后台任务。
如果我错了请纠正我,但我偶然发现 Xamarin 的 a perfect article 讨论了 iOS 后台功能。
我会简单地分成两部分,ios pre 7 和 ios 7+:
iOS 7 之前的版本
The answer is simply 600
seconds (10 minutes), reason is provided by
the article above.
iOS 版本 7+
The answer is that the time system allocates you is opportunistic. You
will have to use @Gary Riches's suggestion
NSLog(@"Time Remaining: %f", [[UIApplication sharedApplication] backgroundTimeRemaining]);
to find out. The reason for it being opportunistic is the way iOS 7+
handles background tasks is completely different, certainly optimised. To
be exact, It has an intermittent behaviour, and therefore, if you need
background tasks such as downloading a big chuck of data, it will be
much more effective if you use `NSURLSession` instead.
但是,在我的特殊情况下,准确地说,我正在上传包含一个文件的单个对象。我不必考虑 NSURLSession
上传少量数据。此外,它是上传任务,它需要多少时间就可以花多少时间。 :-)
对于这些 TL;DR
访问者,上面的答案应该足够了。更详细的可以参考上面的文章
不固定:在Xcode 10 和 Swift4.1
func applicationDidEnterBackground(_ application: UIApplication) {
// Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
// If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
print(UIApplication.shared.backgroundTimeRemaining)
}
OP1:166.13057912699878 平均约 2.7 分钟,
OP2:177.4997792619979 平均约 2.95 分钟
我必须将一组图像文件上传到数据库,因此,我偶然发现 Apple's background execution guide 以确保当用户暂停或终止我的应用程序时应用程序仍会上传数据。
但是在描述中,如果我们调用beginBackgroundTaskWithName:expirationHandler:
或beginBackgroundTaskWithExpirationHandler:
来启动后台任务,它会说giving it a little extra time to finish its work
。
little extra time
到底有多长?
时间量会因许多不同的变量而有所不同,但可以参考 UIApplication
:
backgroundTimeRemaining
属性 检查该值
NSLog(@"Time Remaining: %f", [[UIApplication sharedApplication] backgroundTimeRemaining]);
理论上,你有 2/3 分钟的时间关闭你想在后台执行的任务,如果你不这样做,你的应用程序可能会被杀死。
之后,您可以调用“beginBackgroundTaskWithExpirationHandler”,您必须做好准备,以防万一 Apple 提供的 'little extra time' 不足以完成您需要完成的任务。
编辑:
When an iOS application goes to the background, are lengthy tasks paused?:
Return from
applicationDidEnterBackground(_:)
as quickly as possible. Your implementation of this method has approximately five seconds to perform any tasks and return. If the method doesn’t return before time runs out, your app is terminated and purged from memory.
来自雷温德利希:
'Again, there are no guarantees and the API documentation doesn’t even give a ballpark number – so don’t rely on this number. You might get 5 minutes or 5 seconds, so your app needs to be prepared for anything!':
http://www.raywenderlich.com/29948/backgrounding-for-ios
您的应用进入后台后的等待时间由 iOS 决定。无法保证您获得的时间,但您可以随时查看 UIApplication 的 backgroundTimeRemaining 属性。这将告诉您还剩多少时间。 一般的、基于观察的共识是,通常你有 10 分钟的时间。同样,没有任何保证,API 文档甚至没有给出大概数字——所以不要依赖这个数字。您可能需要 5 分钟或 5 秒,因此您的应用需要为任何事情做好准备!
如果你想在应用程序处于后台时上传你的文件,你应该使用苹果的后台服务。 iOS 将为您的应用程序提供大约的时间。 3 分钟(根据一些经验)完成你的任务,然后它会杀死你的应用程序。
Apple 允许在特殊情况下使用更长的 运行 应用程序。为此,您需要在 info.plist 文件中使用 UIBackgroundModes。有关这些特殊情况的更多信息,请参阅 table 3-1 on this link.
Here 是一篇很好的文章,描述了后台任务 运行 时间以及如何在 iOS 中实现长时间的 运行ning 后台任务。
如果我错了请纠正我,但我偶然发现 Xamarin 的 a perfect article 讨论了 iOS 后台功能。
我会简单地分成两部分,ios pre 7 和 ios 7+:
iOS 7 之前的版本
The answer is simply
600
seconds (10 minutes), reason is provided by the article above.
iOS 版本 7+
The answer is that the time system allocates you is opportunistic. You will have to use @Gary Riches's suggestion
NSLog(@"Time Remaining: %f", [[UIApplication sharedApplication] backgroundTimeRemaining]); to find out. The reason for it being opportunistic is the way iOS 7+ handles background tasks is completely different, certainly optimised. To be exact, It has an intermittent behaviour, and therefore, if you need background tasks such as downloading a big chuck of data, it will be much more effective if you use `NSURLSession` instead.
但是,在我的特殊情况下,准确地说,我正在上传包含一个文件的单个对象。我不必考虑 NSURLSession
上传少量数据。此外,它是上传任务,它需要多少时间就可以花多少时间。 :-)
对于这些 TL;DR
访问者,上面的答案应该足够了。更详细的可以参考上面的文章
不固定:在Xcode 10 和 Swift4.1
func applicationDidEnterBackground(_ application: UIApplication) {
// Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
// If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
print(UIApplication.shared.backgroundTimeRemaining)
}
OP1:166.13057912699878 平均约 2.7 分钟, OP2:177.4997792619979 平均约 2.95 分钟