Skip to content

Instantly share code, notes, and snippets.

@aryaxt
Last active October 14, 2017 08:37
Show Gist options
  • Save aryaxt/5b6c181d5ed5fd7e3bef to your computer and use it in GitHub Desktop.
Save aryaxt/5b6c181d5ed5fd7e3bef to your computer and use it in GitHub Desktop.
Swift Multi threading operator
// 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