NSOperation 队列在后台不工作

Posted

技术标签:

【中文标题】NSOperation 队列在后台不工作【英文标题】:NSOperation queue not working in background 【发布时间】:2015-06-20 14:40:51 【问题描述】:

我希望我的图片上传应该在后台工作。 我已启用后台模式并创建了一个操作队列以一次上传多个文件到服务器。 但是一旦将应用程序置于后台 NSOperation 就会暂停,并在前台重新启动。 我还在开始我的操作队列之前添加了 beginBackgroundTaskWithExpirationHandler,并在所有图像上传到服务器时添加了 endBackgroundTask

这使得所有图像也可以在应用程序处于后台时上传到服务器,但是我的应用程序被杀死了,我不希望应用程序被杀死。

请为我的问题提出一些解决方案。

【问题讨论】:

【参考方案1】:

您的应用在后台运行时的时间有限。在其分配的时间到期后,它会被杀死。

根据苹果:

In your own expiration handlers, you can include additional code needed
to close out your task. However, any code you include must not take too 
long to execute because, by the time your expiration handler is called,
your app is already very close to its time limit. For this reason,
perform only minimal cleanup of your state information and end the
task.

同时获取backgroundTimeRemaining属性的值来查看剩余时间。

【讨论】:

我的任务完成后,我结束了后台任务,但我希望我的应用程序继续在后台工作。我应该怎么做才能不杀死我的应用程序?

以上是关于NSOperation 队列在后台不工作的主要内容,如果未能解决你的问题,请参考以下文章

核心数据并发(NSOperation)

ios开发多线程一:了解-NSOperation的基本使用

iOS开发NSOperation 三:操作依赖和监听以及线程间通信

Ios 多线程之NSOperation与NSOprationQueue

dispatch_async 与 NSOperation 队列

NSOperation创建队列