当iOS应用程序进入后台时,是否会暂停冗长的任务? [英] When an iOS application goes to the background, are lengthy tasks paused?

查看:88
本文介绍了当iOS应用程序进入后台时,是否会暂停冗长的任务?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是的,我知道如果我希望我的应用能够响应用户的多任务操作,例如切换到另一个应用,我应该处理

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?

例如,我的应用程序正在执行

For e.g., my app is doing

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

当i == 500K时,用户切换到另一个应用程序。我的应用程序中的for循环会发生什么?

When i== 500K, user switches to another app. What happens to the for-loop in my app?

推荐答案

来自 iOS应用程序编程指南


你的app委托的 applicationDidEnterBackground:方法有
大约5秒完成任何任务并返回。在实践中,
此方法应尽快返回。如果该方法在时间用完之前没有返回
,那么您的应用程序将被终止并从
内存中清除。如果您仍需要更多时间来执行任务,请调用
beginBackgroundTaskWithExpirationHandler:方法来请求后台
执行时间,然后启动任何长时间运行的任务次要的
线程。无论你是否开始任何后台任务,
applicationDidEnterBackground:方法仍必须在5
秒内退出。

Your app delegate’s applicationDidEnterBackground: method has approximately 5 seconds to finish any tasks and return. 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. 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. Regardless of whether you start any background tasks, the applicationDidEnterBackground: method must still exit within 5 seconds.

如果您在上面描述的长时间运行操作在主线程上,并且在您的应用程序转到后台后需要超过5秒才能完成,您的应用程序将被终止。主线程将被阻止,您将无法及时从 -applicationDidEnterBackground:返回。

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. 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). 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.

这篇关于当iOS应用程序进入后台时,是否会暂停冗长的任务?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆