欢迎来到代码驿站!

当前位置:首页 >

Swift4使用GCD实现计时器

时间:2021-03-04 13:53:44|栏目:|点击:

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

上一篇:清除中国网络游戏木马外挂黑客技术大全

栏    目:

下一篇:Powershell小技巧之通过EventLog查看近期电脑开机和关机时间

本文标题:Swift4使用GCD实现计时器

本文地址:http://www.codeinn.net/misctech/74271.html

推荐教程

广告投放 | 联系我们 | 版权申明

重要申明:本站所有的文章、图片、评论等,均由网友发表或上传并维护或收集自网络,属个人行为,与本站立场无关。

如果侵犯了您的权利,请与我们联系,我们将在24小时内进行处理、任何非本站因素导致的法律后果,本站均不负任何责任。

联系QQ:914707363 | 邮箱:codeinn#126.com(#换成@)

Copyright © 2020 代码驿站 版权所有