Swift4使用GCD实现计时器
更新时间:2020年03月02日 09:28:26 作者:AFengM
这篇文章主要为大家详细介绍了Swift4使用GCD实现计时器,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
开发过程中,我们可能会经常使用到计时器。苹果为我们提供了Timer。但是在平时使用过程中会发现使用Timer会有许多的不便
1:必须保证在一个活跃的runloop,我们知道主线程的runloop是活跃的,但是在其他异步线程runloop就需要我们自己去开启,非常麻烦。
2:Timer的创建和销毁必须在同一个线程。跨线程就操作不了
3:内存问题。可能循环引用造成内存泄露
由于存在上述问题,我们可以采用GCD封装来解决。
import UIKit typealias ActionBlock = () -> () class MRGCDTimer: NSObject { static let share = MRGCDTimer() lazy var timerContainer = [String : DispatchSourceTimer]() /// 创建一个名字为name的定时 /// /// - Parameters: /// - name: 定时器的名字 /// - timeInterval: 时间间隔 /// - queue: 线程 /// - repeats: 是否重复 /// - action: 执行的操作 func scheduledDispatchTimer(withName name:String?, timeInterval:Double, queue:DispatchQueue, repeats:Bool, action:@escaping ActionBlock ) { if name == nil { return } var timer = timerContainer[name!] if timer==nil { timer = DispatchSource.makeTimerSource(flags: [], queue: queue) timer?.resume() timerContainer[name!] = timer } timer?.schedule(deadline: .now(), repeating: timeInterval, leeway: .milliseconds(100)) timer?.setEventHandler(handler: { [weak self] in action() if repeats==false { self?.destoryTimer(withName: name!) } }) } /// 销毁名字为name的计时器 /// /// - Parameter name: 计时器的名字 func destoryTimer(withName name:String?) { let timer = timerContainer[name!] if timer == nil { return } timerContainer.removeValue(forKey: name!) timer?.cancel() } /// 检测是否已经存在名字为name的计时器 /// /// - Parameter name: 计时器的名字 /// - Returns: 返回bool值 func isExistTimer(withName name:String?) -> Bool { if timerContainer[name!] != nil { return true } return false } }
使用方法
MRGCDTimer.share.scheduledDispatchTimer(withName: "name", timeInterval: 1, queue: .main, repeats: true) { //code self.updateCounter() }
取消计时器
MRGCDTimer.share.destoryTimer(withName: "name")
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。
相关文章
RxSwift发送及订阅 Subjects、Variables代码示例
这篇文章主要介绍了RxSwift发送及订阅 Subjects、Variables代码示例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下2020-12-12Swift之UITabBarController 导航控制器的自定义
本文给大家介绍swift导航控制器之UITabBarController,本文通过代码实例给大家讲解swift导航控制器,导航控制器类继承UITabBarController,代码简单易懂,需要的朋友可以参考下2015-10-10
最新评论