Indexed by TopGit from live GitHub metadata: duemunk/Async has 4.5k stars, written primarily in Swift. Syntactic sugar in Swift for asynchronous dispatches in Grand Central Dispatch
Snapshot summary built from the project's own GitHub metadata — there's no written TopGit review yet. The page will update automatically when a full review is published.
WHY NO REVIEW YET
TopGit writes full reviews for the most-starred, most-requested repositories. This page is a snapshot until then — see the READ ME tab for the original README in full.
So even though GCD has nice-ish syntax as of Swift 3.0, compare the above with:
DispatchQueue.global(qos: .userInitiated).async {
let value = 10
DispatchQueue.global(qos: .background).async {
let text = "Score: \(value)"
DispatchQueue.main.async {
label.text = text
}
}
}
AsyncGroup sugar looks like this:
let group = AsyncGroup()
group.background {
print("This is run on the background queue")
}
group.background {
print("This is also run on the background queue in parallel")
}
group.wait()
print("Both asynchronous blocks are complete")
let backgroundBlock = Async.background {
print("This is run on the background queue")
}
// Run other code here...
// Chain to reference
backgroundBlock.main {
print("This is run on the \(qos_class_self().description) (expected \(qos_class_main().description)), after the previous block")
}
let seconds = 0.5
Async.main(after: seconds) {
print("Is called after 0.5 seconds")
}.background(after: 0.4) {
print("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 work
for i in 0...1000 {
print("A \(i)")
}
}
let block2 = block1.background {
print("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:
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.
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.
Apply
There is also a wrapper for dispatch_apply() for quick parallelisation of a for loop.
Apply.background(100) { i in
// Do stuff e.g. print(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.background { Apply.background(100) { ... } }.
AsyncGroup
AsyncGroup facilitates working with groups of asynchronous blocks.
Multiple dispatch blocks with GCD:
let group = AsyncGroup()
group.background {
// Run on background queue
}
group.utility {
// Run on utility queue, in parallel to the previous block
}
group.wait()
let customQueue = dispatch_queue_create("Label", DISPATCH_QUEUE_CONCURRENT)
group.custom(queue: customQueue) {}
Wait for group to finish:
let group = AsyncGroup()
group.background {
// Do stuff
}
group.background {
// Do other stuff in parallel
}
// Wait for both to finish
group.wait()
// Do rest of stuff
Custom asynchronous operations:
let group = AsyncGroup()
group.enter()
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0)) {
// Do stuff
group.leave()
}
group.enter()
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0)) {
// Do other stuff in parallel
group.leave()
}
// Wait for both to finish
group.wait()
// Do rest of stuff
License
The MIT License (MIT)
Copyright (c) 2016 Tobias Due Munk
Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
the Software, and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
No homepage URL was recorded for duemunk/Async in TopGit's last sync. The README tab above frequently contains screenshots and demo links, or check the repository description on GitHub.
Is duemunk/Async open source?
Yes — duemunk/Async ships under the MIT license, which makes its source code freely readable (and, depending on license terms, forkable and reusable). Source: github.com/duemunk/Async.
What is duemunk/Async?
duemunk/Async (duemunk/Async) is a Swift project on GitHub. From the project's own README: Syntactic sugar in Swift for asynchronous dispatches in Grand Central Dispatch
What license does duemunk/Async use?
duemunk/Async is released under the MIT license. Always verify the LICENSE file directly on GitHub for the authoritative terms — license strings can be edited out of sync with a project's actual stance.
Where do I read more about duemunk/Async?
This TopGit page is a snapshot — the READ ME tab shows the project's own README content (links stripped, images preserved). The GitHub repository at github.com/duemunk/Async is the definitive source.
Read full README in the tab above.
Want a second opinion on Async?
Ask an AI that can read this page — one click and you get its take on Async.