Created
March 10, 2016 14:13
-
-
Save kvendrik/f69403cbb0b01577ad2e to your computer and use it in GitHub Desktop.
Shorthand to put stuff on bg thread in Swift
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
| //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