Wednesday, May 11, 2016

Swift sleep function

In swift, sleep is going to block the main thread and make the app unresponsive... You can use NSTimer but the fire time is not that accurate... I prefer:
let seconds = 3.0
let delay = seconds * Double(NSEC_PER_SEC)
let dispatchTime = dispatch_time(DISPATCH_TIME_NOW, Int64(delay))
dispatch_after(dispatchTime, dispatch_get_main_queue(), {
     // code to be executed after the delay
})

No comments:

Post a Comment