Created
June 5, 2014 20:12
-
-
Save pcperini/ae126c5d7108efab9b43 to your computer and use it in GitHub Desktop.
Chain blocks for asynchronous, callback-based execution. Always ends in main queue.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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