时间:2022-08-12 10:02:33 | 栏目:iOS代码 | 点击:次
说起计时器,很多开发人员第一时间就会想起Timer,但是随着使用的深入,慢慢就发现Timer其实不是很好用,比如说TableView滑动时候不执行,Timer循环应用。
DispatchSourceTimer,也就是大家通常叫的GCD Timer,是依赖于GCD的一种Timer,Runloop的底层代码中也用到这种Timer,可见GCD Timer并不依赖与Runloop。
先看一下苹果的定义:
A dispatch source that submits the event handler block based on a timer.
使用下面的方法即可创建一个DispatchSourceTimer对象。
class func makeTimerSource(flags: DispatchSource.TimerFlags = [], queue: DispatchQueue? = nil) -> DispatchSourceTimer // 默认在主队列中调度使用 let timer = DispatchSource.makeTimerSource() // 指定在主队列中调度使用 let timer = DispatchSource.makeTimerSource(flags: [], queue: DispatchQueue.main) // 指定在全局队列中调度使用 let timer = DispatchSource.makeTimerSource(flags: [], queue: DispatchQueue.global()) // 指定在自定义队列中调度使用 let customQueue = DispatchQueue(label: "customQueue") let timer = DispatchSource.makeTimerSource(flags: [], queue: customQueue)
配置Timer参数,需要使用DispatchSourceTimer协议的方法。可以安排一次或多次触发的Timer。Timer每次触发的时候,都会调用已部署的任务。
// 从现在开始,每秒执行一次。
timer?.schedule(deadline: DispatchTime.now(), repeating: .seconds(1), leeway: .nanoseconds(1))
// 5秒之后执行任务,不重复。
timer?.schedule(deadline: DispatchTime.now() + 5, repeating: .never, leeway: .nanoseconds(1))
当Timer配置完参数后,使用DispatchSourceProtocol协议的方法来部署要执行的任务。
setEventHandler和setRegistrationHandler的区别:
例如下面的代码:
var timer: DispatchSourceTimer?
func initTimer() {
// 默认在主队列中调度使用
timer = DispatchSource.makeTimerSource()
// 从现在开始,每秒执行一次。
timer?.schedule(deadline: DispatchTime.now(), repeating: .seconds(1), leeway: .nanoseconds(1))
// 5秒之后执行任务,不重复。
// timer?.schedule(deadline: DispatchTime.now() + 5, repeating: .never, leeway: .nanoseconds(1))
timer?.setEventHandler {
DispatchQueue.main.async {
print("执行任务")
}
}
timer?.setRegistrationHandler(handler: {
DispatchQueue.main.async {
print("Timer开始工作了")
}
})
timer?.activate()
}
执行结果如下:
2020-11-28 02:20:00 +0000 Timer开始工作了
2020-11-28 02:20:00 +0000 执行任务
2020-11-28 02:20:01 +0000 执行任务
2020-11-28 02:20:02 +0000 执行任务
下面看一下Timer的一下控制方法及状态:
上面的这些方法如果使用不当,很容易造成APP崩溃,下面来看一下具体注意事项及建议:
比如:我们需要一定时间情况下(数组长度*4),每隔一段时间打印对应下标(每个元素隔4秒打印),无限打印
在下面例子的双重循环中使用 DispatchSourceTimer 你会发现print只打印了 dom some = 0 这个不是我们想要的效果
var exaltedTimer: DispatchSourceTimer?
func demo {
let arr = [1,2,3,4]
self.exaltedTimer = DispatchSource.makeTimerSource()
self.exaltedTimer?.schedule(deadline: .now(), repeating: TimeInterval(arr.count*4))
self.exaltedTimer?.setEventHandler(handler: {
for (index, item) in arr.enumerated() {
let timer2 = DispatchSource.makeTimerSource()
timer2.schedule(deadline: .now()+4.0*CGFloat(index), repeating: .infinity)
timer2.setEventHandler {
DispatchQueue.main.async {
print("do some = \(index)")
}
}
timer2.activate()
}
})
self.exaltedTimer?.activate()
}
这个是因为timer2使用之后就被释放了,所以要cancel和resume配合使用,这样就实现了我们想要的效果了。
var exaltedTimer: DispatchSourceTimer?
var exaltedTimerArray: [DispatchSourceTimer] = []
func demo {
self.exaltedTimer?.cancel()
for subTimer in self.exaltedTimerArray {
subTimer.cancel()
}
let arr = [1,2,3,4]
self.exaltedTimer = DispatchSource.makeTimerSource()
self.exaltedTimer?.schedule(deadline: .now(), repeating: TimeInterval(arr.count*4))
self.exaltedTimer?.setEventHandler(handler: {
for (index, item) in arr.enumerated() {
let timer2 = DispatchSource.makeTimerSource()
timer2.schedule(deadline: .now()+4.0*CGFloat(index), repeating: .infinity)
timer2.setEventHandler {
DispatchQueue.main.async {
print("do some = \(index)")
}
}
self.exaltedTimerArray.append(timer2)
timer2.resume()
}
})
self.exaltedTimer?.resume()
https://blog.csdn.net/guoyongming925/article/details/110224064