如何在GCD隊(duì)列中停止、取消、掛起、重新開始任務(wù)

上周五在面試的時(shí)候被面試官問到一個(gè)問題:在Swift 3中如何取消一個(gè)正在執(zhí)行的GCD線程?因?yàn)槲沂褂肧wift的時(shí)間還不長(zhǎng),這方面的知識(shí)點(diǎn)我還沒覆蓋到?;丶液蠼?jīng)過一番Google我終于找到了答案。
??答案是我在Stack Overflow上找到的,原問題地址為:http://stackoverflow.com/questions/29492707/how-to-stop-cancel-suspend-resume-tasks-on-gcd-queue
??回答原文摘抄如下:

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).
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.
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:)
](https://developer.apple.com/reference/dispatch/dispatchqueue/2300059-init).
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()
](https://developer.apple.com/reference/dispatch/dispatchworkitem/1780910-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()
](https://developer.apple.com/reference/dispatch/1431046-dispatch_block_testcancel?language=objc) in Objective-C and Swift 2.3, or [item.isCancelled
](https://developer.apple.com/reference/dispatch/dispatchworkitem/1780829-iscancelled) in Swift 3.
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](https://developer.apple.com/videos/wwdc/2014/?id=716), 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.
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
](https://developer.apple.com/reference/foundation/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).
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.
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 基于自 raywenderlich.com 在2015年的兩篇文章 Grand Central Dispatch ...
    seedante閱讀 1,457評(píng)論 0 7
  • __block和__weak修飾符的區(qū)別其實(shí)是挺明顯的:1.__block不管是ARC還是MRC模式下都可以使用,...
    LZM輪回閱讀 3,592評(píng)論 0 6
  • 學(xué)習(xí)多線程,轉(zhuǎn)載兩篇大神的帖子,留著以后回顧!第一篇:關(guān)于iOS多線程,你看我就夠了 第二篇:GCD使用經(jīng)驗(yàn)與技巧...
    John_LS閱讀 732評(píng)論 0 3
  • ———————————————回答好下面的足夠了---------------------------------...
    恒愛DE問候閱讀 1,842評(píng)論 0 4
  • 漢末董卓弄權(quán),諸侯蜂起,十八路諸侯聯(lián)盟攻打董卓,董卓兵敗,無計(jì)可施,只好將朝廷遷都長(zhǎng)安,臨走時(shí)將朝中與諸侯有聯(lián)系的...
    目送歸鴻閱讀 782評(píng)論 2 1

友情鏈接更多精彩內(nèi)容