当应用程序在iOS中进入非活动/后台/挂起状态时,调度队列中的任务会发生什么? [英] What happens to tasks in dispatch queues when an app enters inactive/background/suspended states in iOS?

查看:152
本文介绍了当应用程序在iOS中进入非活动/后台/挂起状态时,调度队列中的任务会发生什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在搜索Apple关于应用程序状态和Grand Central Dispatch的文档,但我没有找到这个问题的好答案。

I've been scouring Apple's documentation on application states and Grand Central Dispatch, but I haven't found a good answer to this question.

根据Apple的文档,在iOS 4.0上:

According to Apple's documentation, on iOS 4.0:


应用程序在后台,但没有执行代码。
系统会自动将应用程序移动到此状态,并在
适当的时间。暂停时,应用程序基本上是
冻结在其当前状态,并且不执行任何代码。
在内存不足的情况下,系统可能会清除暂停的
申请,恕不另行通知,以便为前景
申请腾出更多空间。

The application is in the background but is not executing code. The system moves an application to this state automatically and at appropriate times. While suspended, an application is essentially freeze-dried in its current state and does not execute any code. During low-memory conditions, the system may purge suspended applications without notice to make more space for the foreground application.

因此,假设系统没有清除挂起的应用程序(挂起 - >未运行转换),那么当前在调度队列中执行的任务会发生什么?短语基本冻干还有很多不足之处 - 冻干了什么?

So assuming the system does not purge a suspended application (suspended -> not running transition), what happens to tasks that are currently executing in a dispatch queue? The phrase "Essentially freeze-dried" leaves much to be desired - exactly what is freeze dried?

我的解释是当应用程序转换回活动状态时,应用程序在暂停时使用的GCD队列需要恢复;根据这种解释,暂停前GCD队列中存在的任务将消失。我的解释是否正确?

My interpretation is whichever GCD queues an app is using at time of suspension will need to be reinstated when the app transitions back to active state; under this interpretation, the tasks existing in pre-suspension GCD queues would disappear. Is my interpretation correct?

推荐答案

当应用程序暂停时,整个过程将被冻结。一旦恢复,您可以依赖恢复过程,就好像什么都没发生一样。您的应用程序的GCD逻辑队列不会消失,它们保留在内存中。 GCD在您的流程中创建的线程为您的队列提供服务,恢复到位,好像什么都没发生一样。

When an app is suspended, the entire process is frozen. You can count on the process resuming as if nothing happened at all once it is resumed. Your apps's GCD logical queues don't go away, they remain as they were in memory. And the threads GCD had created in your process to service your queues are resumed in place as if nothing happened as well.

所以你的解释是错误的:预先存在的任务暂停GCD队列在恢复时不会消失。他们从未离开;他们只是暂停了。

So your interpretation is incorrect: tasks existing in pre-suspension GCD queues do not disappear upon resumption. They never went away; they were only paused.

这篇关于当应用程序在iOS中进入非活动/后台/挂起状态时,调度队列中的任务会发生什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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