Simplifying Swift Asynchronous Dispatch in GCD

Working with dispatch_async is not necessarily difficult, however, the code is typically quite verbose(and lengthy). Tobias Due Munk has published an open source project that provides “syntactic sugar in Swift for asynchronous dispatches.

An example from the github project follows.

A call with dispatch_async that looks like this:

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(), 0), {
        println("This is run on the main queue, after the previous block")
    })
})

becomes this:

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

The project includes support for chaining blocks, custom queues, canceling blocks, among others.