日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

[swift] Async

發(fā)布時間:2025/4/9 编程问答 21 豆豆
生活随笔 收集整理的這篇文章主要介紹了 [swift] Async 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

Async

https://github.com/duemunk/Async

?

Syntactic sugar in Swift for asynchronous dispatches in Grand Central Dispatch (GCD)

這是一個Swift中GCD的語法糖庫。

?

Async?sugar looks like this:

Async使用起來就像這樣子:

Async.background {println("This is run on the background queue") }.main {println("This is run on the main queue, after the previous block") }

Instead of the familiar syntax for GCD:

替換了下面的這種顯示方式:

dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0), {println("This is run on the background queue")dispatch_async(dispatch_get_main_queue(), {println("This is run on the main queue, after the previous block") }) })

Install

pod 'Async', :git => 'https://github.com/duemunk/Async.git'

?

Benefits

  • Less verbose code 更少的冗余代碼
  • Less code indentation 更少的縮進(jìn)風(fēng)
  • ?

    Things you can do

    Supports the modern queue classes:

    支持常用的queue類:

    Async.main {} Async.userInteractive {} Async.userInitiated {} Async.utility {} Async.background {}

    Chain as many blocks as you want:

    你可以將不同的block鏈接起來使用:

    Async.userInitiated {// 1 }.main {// 2 }.background {// 3 }.main { // 4 }

    Store reference for later chaining:

    也可以分開使用:

    let backgroundBlock = Async.background {println("This is run on the background queue") } // Run other code here... // Chain to reference backgroundBlock.main { println("This is run on the \(qos_class_self().description) (expected \(qos_class_main().description)), after the previous block") }

    Custom queues:

    自定義queue:

    let customQueue = dispatch_queue_create("CustomQueueLabel", DISPATCH_QUEUE_CONCURRENT) let otherCustomQueue = dispatch_queue_create("OtherCustomQueueLabel", DISPATCH_QUEUE_CONCURRENT) Async.customQueue(customQueue) { println("Custom queue") }.customQueue(otherCustomQueue) { println("Other custom queue") }

    Dispatch block after delay:

    延時執(zhí)行:

    let seconds = 0.5 Async.main(after: seconds) { println("Is called after 0.5 seconds") }.background(after: 0.4) { println("At least 0.4 seconds after previous block, and 0.9 after Async code is called") }

    Cancel blocks that aren't already dispatched:

    取消沒有啟動的線程:

    // Cancel blocks not yet dispatched let block1 = Async.background {// Heavy workfor i in 0...1000 { println("A \(i)") } } let block2 = block1.background { println("B – shouldn't be reached, since cancelled") } Async.main { // Cancel async to allow block1 to begin block1.cancel() // First block is _not_ cancelled block2.cancel() // Second block _is_ cancelled }

    Wait for block to finish – an ease way to continue on current queue after background task:

    等待一個block運(yùn)行結(jié)束:

    let block = Async.background {// Do stuff }// Do other stuffblock.wait()

    ?

    How does it work

    The way it work is by using the new notification API for GCD introduced in OS X 10.10 and iOS 8. Each chaining block is called when the previous queue has finished.

    本庫使用了 iOS 8 提供的通知 API 來完成相關(guān)功能,每一個block都會在上一個block執(zhí)行完了之后繼續(xù)執(zhí)行:

    let previousBlock = {} let chainingBlock = {} let dispatchQueueForChainingBlock = ... // Use the GCD API to extend the blocks let _previousBlock = dispatch_block_create(DISPATCH_BLOCK_INHERIT_QOS_CLASS, previousBlock) let _chainingBlock = dispatch_block_create(DISPATCH_BLOCK_INHERIT_QOS_CLASS, chainingBlock) // Use the GCD API to call back when finishing the "previous" block dispatch_block_notify(_previousBlock, dispatchQueueForChainingBlock, _chainingBlock)

    The syntax part of the chaining works by having class methods on the?Async?object e.g.?Async.main {}?which returns a struct. The struct has matching methods e.g.?theStruct.main {}.

    ?

    Known bugs

    Modern GCD queues don't work as expected in the iOS Simulator. See issues?13,?22.

    ?

    Known improvements

    The?dispatch_block_t?can't be extended. Workaround used: Wrap?dispatch_block_t?in a struct that takes the block as a property.

    ?

    Bonus stuff

    There is also a wrapper for?dispatch_apply()?for quick parallelisation of a?for?loop.

    Apply.background(100) { i in// Do stuff e.g. println(i) }

    Note that this function returns after the block has been run all 100 times i.e. it is not asynchronous. For asynchronous behaviour, wrap it in a an?Async?block like?Async.main{ Apply.background(100) { ... } }.

    ?

    ?

    ?

    轉(zhuǎn)載于:https://www.cnblogs.com/YouXianMing/p/4890645.html

    總結(jié)

    以上是生活随笔為你收集整理的[swift] Async的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。