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

上周五在面試的時候被面試官問到一個問題:在Swift 3中如何取消一個正在執(zhí)行的GCD線程悼泌?因為我使用Swift的時間還不長松捉,這方面的知識點我還沒覆蓋到匪蟀≡芊ⅲ回家后經(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)容合作請聯(lián)系作者
  • 序言:七十年代末艘狭,一起剝皮案震驚了整個濱河市,隨后出現(xiàn)的幾起案子哥纫,更是在濱河造成了極大的恐慌悬秉,老刑警劉巖,帶你破解...
    沈念sama閱讀 218,525評論 6 507
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件械媒,死亡現(xiàn)場離奇詭異目锭,居然都是意外死亡,警方通過查閱死者的電腦和手機纷捞,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,203評論 3 395
  • 文/潘曉璐 我一進(jìn)店門痢虹,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人主儡,你說我怎么就攤上這事奖唯。” “怎么了糜值?”我有些...
    開封第一講書人閱讀 164,862評論 0 354
  • 文/不壞的土叔 我叫張陵丰捷,是天一觀的道長。 經(jīng)常有香客問我寂汇,道長病往,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 58,728評論 1 294
  • 正文 為了忘掉前任骄瓣,我火速辦了婚禮荣恐,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘累贤。我一直安慰自己叠穆,他們只是感情好,可當(dāng)我...
    茶點故事閱讀 67,743評論 6 392
  • 文/花漫 我一把揭開白布臼膏。 她就那樣靜靜地躺著硼被,像睡著了一般。 火紅的嫁衣襯著肌膚如雪渗磅。 梳的紋絲不亂的頭發(fā)上嚷硫,一...
    開封第一講書人閱讀 51,590評論 1 305
  • 那天检访,我揣著相機與錄音,去河邊找鬼仔掸。 笑死脆贵,一個胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的起暮。 我是一名探鬼主播卖氨,決...
    沈念sama閱讀 40,330評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼负懦!你這毒婦竟也來了筒捺?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 39,244評論 0 276
  • 序言:老撾萬榮一對情侶失蹤纸厉,失蹤者是張志新(化名)和其女友劉穎系吭,沒想到半個月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體颗品,經(jīng)...
    沈念sama閱讀 45,693評論 1 314
  • 正文 獨居荒郊野嶺守林人離奇死亡肯尺,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,885評論 3 336
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現(xiàn)自己被綠了躯枢。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片则吟。...
    茶點故事閱讀 40,001評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖闺金,靈堂內(nèi)的尸體忽然破棺而出逾滥,到底是詐尸還是另有隱情,我是刑警寧澤败匹,帶...
    沈念sama閱讀 35,723評論 5 346
  • 正文 年R本政府宣布寨昙,位于F島的核電站,受9級特大地震影響掀亩,放射性物質(zhì)發(fā)生泄漏舔哪。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點故事閱讀 41,343評論 3 330
  • 文/蒙蒙 一槽棍、第九天 我趴在偏房一處隱蔽的房頂上張望捉蚤。 院中可真熱鬧,春花似錦炼七、人聲如沸缆巧。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,919評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽陕悬。三九已至,卻和暖如春按傅,著一層夾襖步出監(jiān)牢的瞬間捉超,已是汗流浹背胧卤。 一陣腳步聲響...
    開封第一講書人閱讀 33,042評論 1 270
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留拼岳,地道東北人枝誊。 一個月前我還...
    沈念sama閱讀 48,191評論 3 370
  • 正文 我出身青樓,卻偏偏與公主長得像惜纸,于是被迫代替她去往敵國和親叶撒。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點故事閱讀 44,955評論 2 355

推薦閱讀更多精彩內(nèi)容

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