Synchronization with the main thread in Swift
class ViewController: UIViewController {
//...
func add(a: Int, _ b: Int) -> Int {
sleep(3)
return a + b
}
func calculate(a: Int, _ b: Int) {
let result = add(a, b)
//synchronization with the main thread
//for update user interface
dispatch_async(dispatch_get_main_queue(), {
self.lResult.text = "\(result)"
})
}
@IBAction func btn_TouchUp(sender: AnyObject) {
//Start new thread
dispatch_async(queue) {
self.calculate(3, 5)
}
}
}
- Log in to post comments