zl程序教程

您现在的位置是:首页 >  前端

当前栏目

Async

2023-09-14 08:57:30 时间

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 更少的缩进风
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:

延时执行:

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 arent already dispatched:

取消没有启动的线程:

// Cancel blocks not yet dispatched

let block1 = Async.background {

 // Heavy work

 for i in 0...1000 {

 println("A \(i)")

let block2 = block1.background {

 println("B – shouldnt 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运行结束:

let block = Async.background {

 // Do stuff

// Do other stuff

block.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 来完成相关功能,每一个block都会在上一个block执行完了之后继续执行:

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 dont work as expected in the iOS Simulator. See issues 1322.

 

Known improvements

The dispatch_block_t cant 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) { ... } }.

 

 


async-await JS查漏补缺系列是我在学习JS高级语法时做的笔记,通过实践费曼学习法进一步加深自己对其的理解,也希望别人能通过我的笔记能学习到相关的知识点。这一次我们来了解JavaScript的async-await
理解 async/await 的执行 这是一篇简单的短文章,方便理解。 开局先丢官宣:sec-async-function-definitions 这个链接是对 await 的解释,解释了它的执行。 await 的执行意味着(官宣巴拉巴拉地说了14点,这里简化成2点): 1. await 以 promise 形式完成,且 await 之后的代码在 promise 被完成后以 .then 执行。
理解 es7 async await 开始进行 react 项目,需要用到 es7 的 async 和 await 来替代以前的 promise 的方法q请求接口。在这里学习一下和大家一起分享。 适用:es6,es7初学者