Monday, July 25, 2016

Swift Infix Custom Threading

Code:
func ~> (task: ()->Void, update: ()-> Void)
{
    let priority = DISPATCH_QUEUE_PRIORITY_DEFAULT
    dispatch_async(dispatch_get_global_queue(priority, 0)) {
        // do some task
        task()
        dispatch_async(dispatch_get_main_queue()) {
            // update some UI
            update()
        }
    }
}

Usage:
 ({
     for i in 0..<100000{
            print("count: \(i)")
         }
     })~>({
         print("Finished counting")
 })

CREDITS:
* Background Threads
* Custom Operators in Swift

0 comments:

Post a Comment