var count =0;
var timer =DispatchSource.makeTimerSource(flags: [], queue:DispatchQueue.global())
timer.setEventHandler {
NSLog("timer exec!")
count += 1
if count >=5 {
timer.cancel()
}
}
timer.scheduleOneshot(deadline: .now())
//timer.scheduleRepeating(deadline: .now(), interval: .seconds(1)/*, leeway: .milliseconds(100)*/)
timer.resume()
在调用DispatchSourceTimer时, 无论设置timer.scheduleOneshot, 还是timer.scheduleRepeating代码 不调用cancel(), 系统会自动调用
另外需要设置全局变量引用, 否则不会调用事件