如何在GCD队列上停止/取消/暂停/恢复任务 [英] How to stop/cancel/suspend/resume tasks on GCD queue

查看:3051
本文介绍了如何在GCD队列上停止/取消/暂停/恢复任务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在GCD队列上停止/取消/暂停/恢复任务

How to stop/cancel/suspend/resume tasks on GCD queue

如何停止后台队列操作?我想在我们的应用程序中停止一些屏幕。有些屏幕应该是自动恢复。那么,如何在iOS中传递队列?

How does one stop background queue operations? I want to stop some screens in our app. And some screens it should be auto resume. So, how does one pass a queue in iOS?

我的意思是当用户浏览应用程序时,我们在dispatch_queue_t中运行后台线程。但它永远不会停止并在代码中恢复。那么如何暂停和恢复队列

I mean when user have browsing the app time we run the background thread in dispatch_queue_t. But it never stops and resume in the code. So how does one suspend and resume a queue

推荐答案

要暂停一个调度队列,它只是 dispatch_suspend(在Objective-C或Swift 2.3中的队列),或者在Swift 3中的 queue.suspend()。这不会影响当前正在运行的任何任务,但只是阻止新任务在该队列上启动。此外,您显然只挂起您创建的队列(不是全局队列,而不是主队列)。

To suspend a dispatch queue, it's simply dispatch_suspend(queue) in Objective-C or Swift 2.3, or queue.suspend() in Swift 3. That doesn't affect any tasks currently running, but merely prevents new tasks from starting on that queue. Also, you obviously only suspend queues that you created (not global queues, not main queue).

要恢复调度队列,它是 dispatch_resume(在Objective-C或Swift 2.3中的队列),或者在Swift 3中的 queue.resume()。没有自动恢复的概念,所以您只需在适当的时候手动恢复它。

To resume a dispatch queue, it's dispatch_resume(queue) in Objective-C or Swift 2.3, or queue.resume() in Swift 3. There's no concept of "auto resume", so you'd just have to manually resume it when appropriate.

要传递调度队列,只需传递 dispatch_queue_t 在Objective-C或Swift 2.3中调用 dispatch_queue_create()时创建的对象,或者在Swift 3中调用 DispatchQueue 使用 DispatchQueue(label :)创建的对象

To pass a dispatch queue around, you simply pass the dispatch_queue_t object that you created when you called dispatch_queue_create() in Objective-C or Swift 2.3, or, in Swift 3, the DispatchQueue object you create with DispatchQueue(label:).

在取消在调度队列中排队的任务方面,这是iOS 8的新功能,您可以调用使用Objective-C中的 dispatch_block_t 对象 dispatch_block_cancel(block)或Swift 2.3,或 item.cancel() Swift 3中的 DispatchWorkItem 。这将取消尚未启动的排队块/项目,但不会停止正在进行的排队/项目。如果您希望能够中断已分派的块/项,则必须定期检查 dispatch_block_testcancel() ,或 item.isCancelled

In terms of canceling tasks queued on dispatch queues, this is a new feature of iOS 8 and you'd call dispatch_block_cancel(block) with your dispatch_block_t object in Objective-C or Swift 2.3, or item.cancel() of a DispatchWorkItem in Swift 3. This cancels queued blocks/items that have not started, but does not stop ones that are underway. If you want to be able to interrupt a dispatched block/item, you have to periodically examine dispatch_block_testcancel() in Objective-C and Swift 2.3, or item.isCancelled in Swift 3.

取消GCD块非常复杂,我可能会将您推荐给WWDC 2014视频功能,性能和诊断:GCD和XPC中的新功能,它向您介绍了调度块对象的概念,对它们进行排队,取消它们等等。这虽然描述了较旧的Objective-C和Swift 2.3 API ,所有概念同样适用于较新的Swift 3 API。

The canceling of GCD blocks is sufficiently complicated that I might refer you to the WWDC 2014 video Power, Performance and Diagnostics: What's new in GCD and XPC, which introduces you to the concept of dispatch block objects, queuing them, canceling them, etc. While this is describing the older Objective-C and Swift 2.3 API, all of the concepts are equally applicable to the newer Swift 3 API.

如果你想取消任务,你也可以考虑在Objective-C或Swift 2.3中使用操作队列, NSOperationQueue ,又名 OperationQueue ,因为它的可取消操作已经存在了一段时间,你可能会找到很多例子线上。它还支持使用 maxConcurrentOperationCount 来约束并发度(而对于调度队列,您只能在串行和并发之间进行选择,并且控制并发性比需要更多努力需要一点点努力您的部分。

If you want to cancel tasks, you might also consider using operation queues, NSOperationQueue in Objective-C or Swift 2.3, a.k.a. OperationQueue in Swift 3, as its cancelable operations have been around for a while and you're likely to find lots of examples online. It also supports constraining the degree of concurrency with maxConcurrentOperationCount (whereas with dispatch queues you can only choose between serial and concurrent, and controlling concurrency more than that requires a tiny bit of effort on your part).

如果使用操作队列,您可以通过更改队列的暂停属性来暂停和恢复。传递它,你只需传递你实例化的 NSOperationQueue 对象。

If using operation queues, you suspend and resume by changing the suspended property of the queue. And to pass it around, you just pass the NSOperationQueue object you instantiated.

说完所有这些之后,我建议你扩展你的问题,详细说明在后台运行什么类型的任务,并阐明你为什么要暂停它们。可能有比挂起后台队列更好的方法。

Having said all of that, I'd suggest you expand your question to elaborate what sort of tasks are running in the background and articulate why you want to suspend them. There might be better approaches than suspending the background queue.

在您的评论中,您提到您使用的是 NSTimer ,又名 计时器 。如果您想停止计时器,请致电 timer.invalidate() 停止它。当你想再次启动时,创建一个新的 NSTimer

In your comments, you mention that you were using NSTimer, a.k.a. Timer in Swift 3. If you want to stop a timer, call timer.invalidate() to stop it. Create a new NSTimer when you want to start it again.

或者如果计时器真的在后台运行线程,GCD定时器做得更优雅,避免了需要在后台线程上创建runloop的愚蠢,你需要在后台线程上运行 NSTimer 。使用GCD计时器,您可以暂停/恢复它,就像暂停/恢复队列一样,只需使用计时器对象而不是队列对象。

Or if the timer is really running on a background thread, GCD timers do this far more gracefully, avoiding the silliness of needing to create a runloop on a background thread, which you need to run NSTimer on background threads. With a GCD timer, you can suspend/resume it just like you suspend/resume a queue, just using the timer object instead of the queue object.

这篇关于如何在GCD队列上停止/取消/暂停/恢复任务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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