将应用程序置于后台时,GCD分派的操作未运行 [英] GCD Dispatched Operation Not Running When App is Placed in Background

查看:69
本文介绍了将应用程序置于后台时,GCD分派的操作未运行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我启动了一种方法,该方法本质上是使用dispatch_queue_create然后使用dispatch_async的无穷循环(然后代码循环位于分派的块内).

I launch a method that is, essentially, an endless loop using dispatch_queue_create and then dispatch_async (and then the code-loop is inside the dispatched block).

循环完美运行.但是,当应用程序进入后台时,它会暂停.然后,当应用程序成为前台时,它将重新启动.

The loop runs perfectly. However, when the application gets backgrounded, it pauses. Then it restarts when the app takes the foreground.

如何防止这种情况发生?我一直在寻找

How can I prevent this from happening? I've been looking here but it seems that the priority is not one of the things I can choose.

推荐答案

使用

Use the -[UIApplication beginBackgroundTaskWithExpirationHandler:] method to start a background task. The OS will give you ten minutes and call the expirationHandler block when it ends.

如果设备无法在后台运行代码或您必须使用其结束运行的任务ID,则此方法将返回 UIBackgroundTaskInvalid .

This method will return UIBackgroundTaskInvalid if the device can't run code in background or the task id that you must use to end it otherwise.

您可以(并且应该)通过调用-[UIApplication endBackgroundTask];

You can (and should) end it sooner by calling -[UIApplication endBackgroundTask];

您可能可以在块的开头开始一个任务,并在结束时结束它,如果它是一个无穷循环,只需在applicationWillResignActive方法中开始一个任务,然后在applicationDidBecomeActive上结束它.但是请记住,您只有十分钟,才能有更多时间使用您的应用来使用位置,音频或语音.

You can probably start a task in the beginning of your block and end it when it ends, if it is a endless loop just start a task in the applicationWillResignActive method and end it on applicationDidBecomeActive. But remember, you only have ten minutes, to have more time your app would have to use location, audio, or voip.

这篇关于将应用程序置于后台时,GCD分派的操作未运行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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