加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

swift3.0 GCD DispatchSourceTimer注意事项

发布时间:2020-12-14 06:39:52 所属栏目:百科 来源:网络整理
导读: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: . n

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以上支持,兼容之前版本代码中注意判断

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读