Last active
October 14, 2017 08:37
-
-
Save aryaxt/5b6c181d5ed5fd7e3bef to your computer and use it in GitHub Desktop.
Swift Multi threading operator
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
// Takes 2 closures. | |
// Executes first closure in background and passes its value to the second closure on the ui thread | |
infix operator ~> {} | |
func ~> <T> (first:() -> T?, second:(result: T?) -> Void) -> Void { | |
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), { () -> Void in | |
var result: T? = first() | |
dispatch_async(dispatch_get_main_queue(), { () -> Void in | |
second(result: result) | |
}) | |
}) | |
} | |
// Usage | |
{ return "Long running task" } ~> { (result: String?) in self.label.text = result } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment