Swift4计时器 Swift4使用GCD实现计时器

软件发布|下载排行|最新软件

当前位置:首页IT学院IT技术

Swift4计时器 Swift4使用GCD实现计时器

AFengM   2021-04-21 我要评论

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

Copyright 2022 版权所有 软件发布 访问手机版

声明:所有软件和文章来自软件开发商或者作者 如有异议 请与本站联系 联系我们