Skip to content

Instantly share code, notes, and snippets.

@pcperini
Created June 5, 2014 20:12
Show Gist options
  • Save pcperini/ae126c5d7108efab9b43 to your computer and use it in GitHub Desktop.
Save pcperini/ae126c5d7108efab9b43 to your computer and use it in GitHub Desktop.
Chain blocks for asynchronous, callback-based execution. Always ends in main queue.
operator infix ~> { associativity right }
@infix func ~>(firstBlock: (() -> Void), secondBlock: (() -> Void)) -> (() -> Void) {
return {
dispatch_async(dispatch_get_global_queue(-32768, 0), {
firstBlock()
dispatch_async(dispatch_get_main_queue(), secondBlock)
})
}
}
// ...
({
println(NSThread.currentThread()) // <NSThread: 0xb508c80>{number = 2, name = (null)}
} ~> {
println(NSThread.currentThread()) // <NSThread: 0xb507b60>{number = 3, name = (null)}
} ~> {
println(NSThread.currentThread()) // <NSThread: 0xb214a70>{number = 1, name = main}
})()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment