开发过程中,我们可能会经常使用到计时器。苹果为我们提供了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 {