<bdo id='IfiQA'></bdo><ul id='IfiQA'></ul>

      <i id='IfiQA'><tr id='IfiQA'><dt id='IfiQA'><q id='IfiQA'><span id='IfiQA'><b id='IfiQA'><form id='IfiQA'><ins id='IfiQA'></ins><ul id='IfiQA'></ul><sub id='IfiQA'></sub></form><legend id='IfiQA'></legend><bdo id='IfiQA'><pre id='IfiQA'><center id='IfiQA'></center></pre></bdo></b><th id='IfiQA'></th></span></q></dt></tr></i><div id='IfiQA'><tfoot id='IfiQA'></tfoot><dl id='IfiQA'><fieldset id='IfiQA'></fieldset></dl></div>
    1. <tfoot id='IfiQA'></tfoot>

      <small id='IfiQA'></small><noframes id='IfiQA'>

      <legend id='IfiQA'><style id='IfiQA'><dir id='IfiQA'><q id='IfiQA'></q></dir></style></legend>
    2. 如何停止/取消/暂停/恢复 GCD 队列上的任务

      How to stop/cancel/suspend/resume tasks on GCD queue(如何停止/取消/暂停/恢复 GCD 队列上的任务)

        <legend id='A1Lqf'><style id='A1Lqf'><dir id='A1Lqf'><q id='A1Lqf'></q></dir></style></legend>
          <tbody id='A1Lqf'></tbody>

      1. <tfoot id='A1Lqf'></tfoot>
          <bdo id='A1Lqf'></bdo><ul id='A1Lqf'></ul>
        • <small id='A1Lqf'></small><noframes id='A1Lqf'>

        • <i id='A1Lqf'><tr id='A1Lqf'><dt id='A1Lqf'><q id='A1Lqf'><span id='A1Lqf'><b id='A1Lqf'><form id='A1Lqf'><ins id='A1Lqf'></ins><ul id='A1Lqf'></ul><sub id='A1Lqf'></sub></form><legend id='A1Lqf'></legend><bdo id='A1Lqf'><pre id='A1Lqf'><center id='A1Lqf'></center></pre></bdo></b><th id='A1Lqf'></th></span></q></dt></tr></i><div id='A1Lqf'><tfoot id='A1Lqf'></tfoot><dl id='A1Lqf'><fieldset id='A1Lqf'></fieldset></dl></div>

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

                问题描述

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

                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?

                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

                解决方案

                To suspend a dispatch queue, it is simply queue.suspend() (dispatch_suspend(queue) in Objective-C). 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).

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

                To pass a dispatch queue around, you simply pass the DispatchQueue object that you created (or the dispatch_queue_t object that you created when you called dispatch_queue_create() in Objective-C).

                In terms of canceling tasks queued on dispatch queues, this is a was introduced in iOS 8. One can item.cancel() a DispatchWorkItem (dispatch_block_cancel(block) a dispatch_block_t object in Objective-C). 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 item.isCancelled (or dispatch_block_testcancel() in Objective-C).

                See https://stackoverflow.com/a/38372384/1271826 for examples on canceling dispatch work items.

                If you want to cancel tasks, you might also consider using operation queues, i.e. OperationQueue (NSOperationQueue in Objective-C). 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).

                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.


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

                Or if the timer is really running on a background thread, GCD "dispatch source timers" do this far more gracefully. 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 队列上的任务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                相关文档推荐

                what#39;s property copy means in Cocoa#39;s Framework?(like UITabBar#39;s items property)(Cocoa 框架中的属性副本是什么意思?(如 UITabBar 的 items 属性))
                Can`t copy file from bundle to documents directory in iOS(无法将文件从捆绑包复制到 iOS 中的文档目录)
                Automatically copy property values from one object to another of a different type but the same protocol (Objective-C)(自动将属性值从一个对象复制到另一个类型不同但协议相同的对象 (Objective-C))
                Copy text to clipboard with iOS(使用 iOS 将文本复制到剪贴板)
                How to copy text to clipboard/pasteboard with Swift(如何使用 Swift 将文本复制到剪贴板/粘贴板)
                How to show the progress of copying a large file in iOS?(如何在iOS中显示复制大文件的进度?)
              • <small id='SE9lN'></small><noframes id='SE9lN'>

                <i id='SE9lN'><tr id='SE9lN'><dt id='SE9lN'><q id='SE9lN'><span id='SE9lN'><b id='SE9lN'><form id='SE9lN'><ins id='SE9lN'></ins><ul id='SE9lN'></ul><sub id='SE9lN'></sub></form><legend id='SE9lN'></legend><bdo id='SE9lN'><pre id='SE9lN'><center id='SE9lN'></center></pre></bdo></b><th id='SE9lN'></th></span></q></dt></tr></i><div id='SE9lN'><tfoot id='SE9lN'></tfoot><dl id='SE9lN'><fieldset id='SE9lN'></fieldset></dl></div>
                  <tfoot id='SE9lN'></tfoot>
                    <tbody id='SE9lN'></tbody>
                  • <legend id='SE9lN'><style id='SE9lN'><dir id='SE9lN'><q id='SE9lN'></q></dir></style></legend>

                        <bdo id='SE9lN'></bdo><ul id='SE9lN'></ul>