iOS的几种定时器(Timer)创建方式

开发过程中,遇到在某个时间或按照某个周期来执行一些方法的时候,就会用到定时器。下面就是几种开发中常见的定时器的创建方式。

1.Timer(NSTimer)

(1) target action:

    func testTimer(){
        let timer = Timer.init(timeInterval: 1, target: self, selector: #selector(timerAction), userInfo: nil, repeats: true)
        // 需要手动加入到runLoop中
        // 如果想不受scrollView页面滑动影响(滑动时不响应selector,滑动停止后恢复),需设置当前runloop的commonMode模式
        RunLoop.current.add(timer, forMode: .default)
    }

    @objc func timerAction(){
        print("timerAction")
    }

(2) scheduledTimer block

// 默认添加到runLoop中,使用defaultMode模式
timer = Timer.scheduledTimer(withTimeInterval: 1, repeats: true, block: { (timer) in
            print("timerAction")
        })
// 可以手动设置当前runloop的commonMode模式避免页面滑动影响
RunLoop.current.add(timer, forMode: .common)

NSTimer的执行依赖runLoop,如果当前runLoop正在执行一个连续性的运算,timer就会被延时触发。当延迟超过timer的一个重复周期,会在延时结束后按照指定的周期继续执行。

2.GCD(DispatchSourceTimer)

let gcdTimer = DispatchSource.makeTimerSource()
gcdTimer.schedule(deadline: DispatchTime.now(), repeating: DispatchTimeInterval.seconds(1))
gcdTimer.setEventHandler(handler: {
    print("GCD timerAction")
})
// 默认挂起状态,需手动启动
gcdTimer.resume()
// 挂起,挂起状态时不能释放(置为nil),会导致崩溃
// 可以再次resume()唤起定时器
gcdTimer.suspend()
// 取消,解释定时任务
// 取消任务后如果想再次执行Timer,只能重新创建一个 Timer
gcdTimer.cancel()
gcdTimer = nil

GCD创建的Timer不受runLoop影响,使用DispatchSource,可以实现更加精准的定时效果。

3.CADisplayLink

let cadTimer = CADisplayLink(target: self, selector: #selector(timerAction))
// 单位是帧,屏幕刷新多少帧时调用一次selector
// 默认值为0,选择使用设备的最高屏幕刷新频率(iOS为60次每秒)
// 所以执行时间间隔为:preferredFramesPerSecond * 最高屏幕刷新间隔,如iOS设备:preferredFramesPerSecond * 1/60 秒
cadTimer.preferredFramesPerSecond = 20
// 注册到runLoop中监听
cadTimer.add(to: RunLoop.current, forMode: .default)
// 挂起
// cadTimer.isPaused = true

// 终止
cadTimer?.invalidate()
cadTimer = nil

由于跟屏幕刷新同步,非常适合UI的重复绘制,如:下载进度条,自定义动画设计,视频播放渲染等。

4.RxSwift(interval/timer)

// interval方式
// period:每次发送的时间间隔,scheduler:调度者
timer = Observable.interval(1, scheduler: MainScheduler.instance)  
// timer方式
//timer = Observable.timer(1, scheduler: MainScheduler.instance)
timer?.subscribe(onNext: { (time) in
        print(time)
})
.disposed(by: disposeBag)

源码解析:
interval,timer创建都是使用以下初始化方式。

    public static func timer(_ dueTime: RxTimeInterval, period: RxTimeInterval? = nil, scheduler: SchedulerType)
        -> Observable {
        return Timer(
            dueTime: dueTime,
            period: period,
            scheduler: scheduler
        )
    }
final private class Timer: Producer {
    fileprivate let _scheduler: SchedulerType
    fileprivate let _dueTime: RxTimeInterval
    fileprivate let _period: RxTimeInterval?

    init(dueTime: RxTimeInterval, period: RxTimeInterval?, scheduler: SchedulerType) {
        self._scheduler = scheduler
        self._dueTime = dueTime
        self._period = period
    }

    override func run(_ observer: Observer, cancel: Cancelable) -> (sink: Disposable, subscription: Disposable) where Observer.Element == Element {
        if self._period != nil {
            let sink = TimerSink(parent: self, observer: observer, cancel: cancel)
            let subscription = sink.run()
            return (sink: sink, subscription: subscription)
        }
        else {
            let sink = TimerOneOffSink(parent: self, observer: observer, cancel: cancel)
            let subscription = sink.run()
            return (sink: sink, subscription: subscription)
        }
    }
}

了解过RxSwift的同学,Timer类是不是很眼熟,是不是和AnonymousObservable非常相似,一样的继承Producer,一样的init方式,一样的run方法中创建sink。有兴趣的同学可以看看这两篇RxSwift(二)原理-执行流程
,RxSwift(三)原理深入探究。

原理和RxSwift核心原理差不多,只是生成了TimerSink或者TimerOneOffSinksink调用run方法,以TimerSink为例:
跟一下源码:sink.run()

final private class TimerSink : Sink where Observer.Element : RxAbstractInteger  {
    func run() -> Disposable {
        return self._parent._scheduler.schedulePeriodic(0 as Observer.Element, startAfter: self._parent._dueTime, period: self._parent._period!) { state in
            self._lock.lock(); defer { self._lock.unlock() }
            self.forwardOn(.next(state))
            return state &+ 1
        }
    }
}
public class SerialDispatchQueueScheduler : SchedulerType {
/**
    Schedules a periodic piece of work.
    
    - parameter state: State passed to the action to be executed.
    - parameter startAfter: Period after which initial work should be run.
    - parameter period: Period for running the work periodically.
    - parameter action: Action to be executed.
    - returns: The disposable object used to cancel the scheduled action (best effort).
    */
    public func schedulePeriodic(_ state: StateType, startAfter: RxTimeInterval, period: RxTimeInterval, action: @escaping (StateType) -> StateType) -> Disposable {
        return self.configuration.schedulePeriodic(state, startAfter: startAfter, period: period, action: action)
    }
}
extension DispatchQueueConfiguration {
func schedulePeriodic(_ state: StateType, startAfter: RxTimeInterval, period: RxTimeInterval, action: @escaping (StateType) -> StateType) -> Disposable {
        let initial = DispatchTime.now() + startAfter

        var timerState = state

        let timer = DispatchSource.makeTimerSource(queue: self.queue)
        timer.schedule(deadline: initial, repeating: period, leeway: self.leeway)
        
        var timerReference: DispatchSourceTimer? = timer
        let cancelTimer = Disposables.create {
            timerReference?.cancel()
            timerReference = nil
        }

        timer.setEventHandler(handler: {
            if cancelTimer.isDisposed {
                return
            }
            timerState = action(timerState)
        })
        timer.resume()
        
        return cancelTimer
    }
}

可以看到这里其实就是初始化了一个DCD Timer,这里有一句关键代码:timerState = action(timerState)
这里的action就是schedulePeriodic传入的闭包:

        { state in
            self._lock.lock(); defer { self._lock.unlock() }
            self.forwardOn(.next(state))
            return state &+ 1
        }

闭包里通过state的递增,重复调用self.forwardOn(.next(state)),这句代码会调用self._observer.on(event),将event传入到subscribe()订阅方法中,订阅方法创建匿名观察者AnonymousObserver保存的闭包会根据传入的event枚举值对应到onNext()

RxSwift的timer是封装的DispatchSource定时器的,所以也是不受runloop影响的。

你可能感兴趣的:(iOS的几种定时器(Timer)创建方式)