swift3.0 GCD DispatchSourceTimer注意事项
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(),事件才会执行 不知道为何苹果这样设计,可能是考虑到开发者可能忘记取消定时器而影响性能 ios 10.0以上支持,兼容之前版本代码中注意判断 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |