简体   繁体   English

当 iOS 应用程序进入后台时,冗长的任务是否暂停?

[英]When an iOS application goes to the background, are lengthy tasks paused?

Yes, I know if I wish my app to be responsive to users' multitasking actions, such as switch to another app, I should deal with是的,我知道如果我希望我的应用程序能够响应用户的多任务操作,例如切换到另一个应用程序,我应该处理

- (void)applicationWillResignActive:(UIApplication *)application
- (void)applicationDidBecomeActive:(UIApplication *)application

What if my app is doing a quite-long time consuming operation (like downloading a big file) and the user causes my app to enter the background?如果我的应用程序正在执行一个非常耗时的操作(例如下载一个大文件)并且用户导致我的应用程序进入后台怎么办? Will that operation automatically be suspended and resumed when the user comes back to my app?当用户回到我的应用程序时,该操作会自动暂停和恢复吗?

What exactly will happen behind the scene when my app enters the background or resumes in the foreground?当我的应用程序进入后台或在前台恢复时,幕后究竟会发生什么?

What if when users let my app go to the background my app's execution is just in the middle of a method?如果当用户让我的应用程序 go 到后台时,我的应用程序的执行只是在一个方法的中间怎么办?

For eg, my app is doing例如,我的应用正在做

for (int i = 1 to 10000K) {
    do some calculation;
}

When i== 500K, user switches to another app.当 i== 500K 时,用户切换到另一个应用程序。 What happens to the for-loop in my app?我的应用程序中的 for 循环会发生什么?

From the iOS App Programming Guide :来自iOS 应用程序编程指南

Your app delegate's applicationDidEnterBackground: method has approximately 5 seconds to finish any tasks and return.您的应用委托的applicationDidEnterBackground:方法大约有 5 秒的时间来完成所有任务并返回。 In practice, this method should return as quickly as possible.在实践中,此方法应尽快返回。 If the method does not return before time runs out, your app is killed and purged from memory.如果该方法在时间用完之前没有返回,您的应用程序将被杀死并从 memory 中清除。 If you still need more time to perform tasks, call the beginBackgroundTaskWithExpirationHandler: method to request background execution time and then start any long-running tasks in a secondary thread.如果您仍然需要更多时间来执行任务,请调用beginBackgroundTaskWithExpirationHandler:方法来请求后台执行时间,然后在辅助线程中启动任何长时间运行的任务。 Regardless of whether you start any background tasks, the applicationDidEnterBackground: method must still exit within 5 seconds.无论您是否启动任何后台任务, applicationDidEnterBackground:方法仍必须在 5 秒内退出。

If the long-running operation you describe above is on the main thread and it takes longer than 5 seconds to finish after your application heads to the background, your application will be killed.如果您上面描述的长时间运行的操作在主线程上,并且在您的应用程序进入后台后需要超过 5 秒才能完成,您的应用程序将被终止。 The main thread will be blocked and you won't have a chance to return from -applicationDidEnterBackground: in time.主线程将被阻塞,您将没有机会及时从-applicationDidEnterBackground:返回。

If your task is running on a background thread (and it really should be, if it's taking long to execute), that thread appears to be paused if the application returns from -applicationDidEnterBackground: (according to the discussion in this answer ).如果您的任务在后台线程上运行(确实应该是,如果执行时间很长),如果应用程序从-applicationDidEnterBackground:返回,则该线程似乎已暂停:(根据此答案中的讨论)。 It will be resumed when the application is brought back to the foreground.当应用程序回到前台时,它将恢复。

However, in the latter case you should still be prepared for your application to be terminated at any time while it's in the background by cleaning things up on your way to the background.但是,在后一种情况下,您仍应准备好在后台随时终止您的应用程序,方法是在前往后台的路上进行清理。

If you are doing some operation which might consume time and you don't want to kill it then you can extend the time for your operation by executing in UIBackground Task i如果您正在执行一些可能会消耗时间的操作并且您不想杀死它,那么您可以通过在 UIBackground Task i 中执行来延长您的操作时间

{
    UIBackgroundTaskIdentifier  taskId = 0;
    taskId = [application beginBackgroundTaskWithExpirationHandler:^{
        taskId = UIBackgroundTaskInvalid;
    }];

// Execute long process. This process will have 10 mins even if your app goes in background mode.

}

The block argument called "handler" is what will happen when the background task expire (10min).称为“处理程序”的块参数是后台任务到期(10 分钟)时会发生的情况。 Here is a link to the documentation这是文档的链接

Like mentioned above, there are a few cases where your app runs in the background and apple can allow or deny depending on what you are doing.如上所述,在某些情况下,您的应用程序在后台运行,Apple 可以根据您的操作允许或拒绝。

https://developer.apple.com/library/ios/documentation/iphone/conceptual/iphoneosprogrammingguide/ManagingYourApplicationsFlow/ManagingYourApplicationsFlow.html https://developer.apple.com/library/ios/documentation/iphone/conceptual/iphoneosprogrammingguide/ManagingYourApplicationsFlow/ManagingYourApplicationsFlow.html

More importantly if you do fit into one of these categories your app refresh rate is determined by an apple algorithm that takes into consideration your app usage on that device vs other apps.更重要的是,如果您确实属于这些类别之一,那么您的应用刷新率是由苹果算法确定的,该算法会考虑您在该设备上的应用与其他应用的使用情况。 If your app is used more often then it gets more background time allotted.如果您的应用程序使用更频繁,那么它会分配更多的后台时间。 This is just one variable but you get the idea that background time allocation varies app to app and not under your control.这只是一个变量,但您会发现后台时间分配因应用程序而异,不受您的控制。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM