Skip to content

Instantly share code, notes, and snippets.

@kvendrik
Created March 10, 2016 14:13
Show Gist options
  • Select an option

  • Save kvendrik/f69403cbb0b01577ad2e to your computer and use it in GitHub Desktop.

Select an option

Save kvendrik/f69403cbb0b01577ad2e to your computer and use it in GitHub Desktop.
Shorthand to put stuff on bg thread in Swift
//https://thatthinginswift.com/background-threads/
import Foundation
//{ /* do some task on bg thread */ } ~> { /* update some UI on the main thread */}
infix operator ~> {}
private let queue = dispatch_queue_create("serial-worker", DISPATCH_QUEUE_SERIAL)
func ~> <R> (
backgroundClosure: () -> R,
mainClosure: (result: R) -> ())
{
dispatch_async(queue){
let result = backgroundClosure()
dispatch_async(dispatch_get_main_queue(), {
mainClosure(result: result)
})
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment