iOS DispatchSourceTimer 定時器

1. 概述

說起計時器,很多開發(fā)人員第一時間就會想起Timer,但是隨著使用的深入,慢慢就發(fā)現(xiàn)Timer其實不是很好用,比如說TableView滑動時候不執(zhí)行,Timer循環(huán)引用。

2. DispatchSourceTimer

DispatchSourceTimer,也就是大家通常叫的GCD Timer,是依賴于GCD的一種Timer,Runloop的底層代碼中也用到這種Timer,可見GCD Timer并不依賴與Runloop。

先看一下蘋果的定義:

A dispatch source that submits the event handler block based on a timer.

2.1 GCD Timer 創(chuàng)建

使用下面的方法即可創(chuàng)建一個DispatchSourceTimer對象。

class func makeTimerSource(flags: DispatchSource.TimerFlags = [], queue: DispatchQueue? = nil) -> DispatchSourceTimer
// 默認在主隊列中調(diào)度使用
let timer = DispatchSource.makeTimerSource()
// 指定在主隊列中調(diào)度使用
let timer = DispatchSource.makeTimerSource(flags: [], queue: DispatchQueue.main)
// 指定在全局隊列中調(diào)度使用
let timer = DispatchSource.makeTimerSource(flags: [], queue: DispatchQueue.global())
// 指定在自定義隊列中調(diào)度使用
let customQueue = DispatchQueue(label: "customQueue")
let timer = DispatchSource.makeTimerSource(flags: [], queue: customQueue)

2.2 GCD Timer 配置

配置Timer參數(shù),需要使用DispatchSourceTimer協(xié)議的方法??梢园才乓淮位蚨啻斡|發(fā)的Timer。Timer每次觸發(fā)的時候,都會調(diào)用已部署的任務(wù)。

    // 從現(xiàn)在開始,每秒執(zhí)行一次。
    timer?.schedule(deadline: DispatchTime.now(), repeating: .seconds(1), leeway: .nanoseconds(1))
    // 5秒之后執(zhí)行任務(wù),不重復(fù)。
    timer?.schedule(deadline: DispatchTime.now() + 5, repeating: .never, leeway: .nanoseconds(1))

2.3 GCD Timer 部署任務(wù)

當Timer配置完參數(shù)后,使用DispatchSourceProtocol協(xié)議的方法來部署要執(zhí)行的任務(wù)。

setEventHandler和setRegistrationHandler的區(qū)別:

  • setEventHandler:給Timer設(shè)置要執(zhí)行的任務(wù),包括一次性任務(wù)和定時重復(fù)的任務(wù)。回調(diào)方法在子線程中執(zhí)行。
  • setRegistrationHandler:這個方法設(shè)置的任務(wù)只會執(zhí)行一次,也就是在Timer就緒后開始運行的時候執(zhí)行,類似于Timer開始的一個通知回調(diào)?;卣{(diào)方法在子線程中執(zhí)行。
    例如下面的代碼:
    var timer: DispatchSourceTimer?
 
    func initTimer() {
        // 默認在主隊列中調(diào)度使用
        timer = DispatchSource.makeTimerSource()
        
        // 從現(xiàn)在開始,每秒執(zhí)行一次。
        timer?.schedule(deadline: DispatchTime.now(), repeating: .seconds(1), leeway: .nanoseconds(1))
        // 5秒之后執(zhí)行任務(wù),不重復(fù)。
//        timer?.schedule(deadline: DispatchTime.now() + 5, repeating: .never, leeway: .nanoseconds(1))
        
        timer?.setEventHandler {
            DispatchQueue.main.async {
                print("執(zhí)行任務(wù)")
            }
        }
        
        timer?.setRegistrationHandler(handler: {
            DispatchQueue.main.async {
                print("Timer開始工作了")
            }
        })
        timer?.activate()
    }

執(zhí)行結(jié)果如下:

2020-11-28 02:20:00 +0000 Timer開始工作了
2020-11-28 02:20:00 +0000 執(zhí)行任務(wù)
2020-11-28 02:20:01 +0000 執(zhí)行任務(wù)
2020-11-28 02:20:02 +0000 執(zhí)行任務(wù)

2.4 GCD Timer控制方法

下面看一下Timer的一下控制方法及狀態(tài):

  • activate() : 當創(chuàng)建完一個Timer之后,其處于未激活的狀態(tài),所以要執(zhí)行Timer,需要調(diào)用該方法。

  • suspend() : 當Timer開始運行后,調(diào)用該方法便會將Timer掛起,即暫停。

  • resume() : 當Timer被掛起后,調(diào)用該方法便會將Timer繼續(xù)運行。

  • cancel() : 調(diào)用該方法后,Timer將會被取消,被取消的Timer如果想再執(zhí)行任務(wù),則需要重新創(chuàng)建。
    上面的這些方法如果使用不當,很容易造成APP崩潰,下面來看一下具體注意事項及建議:

  • 當Timer創(chuàng)建完后,建議調(diào)用activate()方法開始運行。如果直接調(diào)用resume()也可以開始運行。

  • suspend()的時候,并不會停止當前正在執(zhí)行的event事件,而是會停止下一次event事件。

  • 當Timer處于suspend的狀態(tài)時,如果銷毀Timer或其所屬的控制器,會導(dǎo)致APP奔潰。

  • suspend()和resume()需要成對出現(xiàn),掛起一次,恢復(fù)一次,如果Timer開始運行后,在沒有suspend的時候,直接調(diào)用resume(),會導(dǎo)致APP崩潰。

  • 使用cancel()的時候,如果Timer處于suspend狀態(tài),APP崩潰。

  • 另外需要注意block的循環(huán)引用問題。

2.5 雙重循環(huán) DispatchSourceTimer

比如:我們需要一定時間情況下(數(shù)組長度*4),每隔一段時間打印對應(yīng)下標(每個元素隔4秒打印),無限打印

在下面例子的雙重循環(huán)中使用 DispatchSourceTimer 你會發(fā)現(xiàn)print只打印了 dom some = 0
這個不是我們想要的效果

var exaltedTimer: DispatchSourceTimer?

func demo {
    let arr = [1,2,3,4]

    self.exaltedTimer = DispatchSource.makeTimerSource()
    self.exaltedTimer?.schedule(deadline: .now(), repeating: TimeInterval(arr.count*4))

    self.exaltedTimer?.setEventHandler(handler: {

        for (index, item) in arr.enumerated() {

            let timer2 = DispatchSource.makeTimerSource()
            timer2.schedule(deadline: .now()+4.0*CGFloat(index), repeating: .infinity)
            timer2.setEventHandler {
                DispatchQueue.main.async {
                    print("do some = \(index)")
                }
            }
            
            timer2.activate()


        }
    })
    self.exaltedTimer?.activate()
}

這個是因為timer2使用之后就被釋放了,所以要cancel和resume配合使用,這樣就實現(xiàn)了我們想要的效果了。

var exaltedTimer: DispatchSourceTimer?

var exaltedTimerArray: [DispatchSourceTimer] = []

func demo {

    self.exaltedTimer?.cancel()
    for subTimer in self.exaltedTimerArray {
        subTimer.cancel()
    }

    let arr = [1,2,3,4]

    self.exaltedTimer = DispatchSource.makeTimerSource()
    self.exaltedTimer?.schedule(deadline: .now(), repeating: TimeInterval(arr.count*4))

    self.exaltedTimer?.setEventHandler(handler: {

        for (index, item) in arr.enumerated() {

            let timer2 = DispatchSource.makeTimerSource()
            timer2.schedule(deadline: .now()+4.0*CGFloat(index), repeating: .infinity)
            timer2.setEventHandler {
                DispatchQueue.main.async {
                    print("do some = \(index)")
                }
            }
            
            self.exaltedTimerArray.append(timer2)
            timer2.resume()


        }
    })
    self.exaltedTimer?.resume()

3 OC GCD定時器 封裝

GCDTimer.h

@interface GCDTimer : NSObject


/**
 * 對象創(chuàng)建定時器 主線程創(chuàng)建
 * interval 定時時間
 * repeat 是否輪詢
 * block 返回
 */
- (instancetype)initWithTimeInterval:(NSTimeInterval)interval repeat:(BOOL)repeat block:(dispatch_block_t)block;

/**
 * 對象創(chuàng)建定時器 主線程創(chuàng)建
 * interval 定時時間
 * repeat 是否輪詢
 * queue 線程
 * block 返回
 */
- (instancetype)initWithTimeInterval:(NSTimeInterval)interval repeat:(BOOL)repeat queue:(dispatch_queue_t)queue block:(dispatch_block_t)block;

/**
 * 類創(chuàng)建定時器 主線程創(chuàng)建
 * interval 定時時間
 * repeat 是否輪詢
 * block 返回
 */
+ (instancetype)scheduledTimerWithTimeInterval:(NSTimeInterval)interval repeat:(BOOL)repeat queue:(dispatch_queue_t)queue block:(dispatch_block_t)block;

/**
 * 類創(chuàng)建定時器 主線程創(chuàng)建
 * interval 定時時間
 * repeat 是否輪詢
 * queue 線程
 * block 返回
 */
+ (instancetype)scheduledTimerWithTimeInterval:(NSTimeInterval)interval repeat:(BOOL)repeat block:(dispatch_block_t)block;

/**
 * 設(shè)置定時器時間
 * interval 定時時間
 */
- (void)setTimeInterval:(NSTimeInterval)interval;

/**
 * 停止定時器
 */
- (void)stop;

/**
 * 重啟定時器
 */
- (void)restart;

/**
 * 釋放定時器
 */
- (void)invalidate;

@end


GCDTimer.m

@interface GCDTimer() {
    dispatch_source_t _timer;
    BOOL _isFire;
}

@end

@implementation GCDTimer

- (instancetype)initWithTimeInterval:(NSTimeInterval)interval repeat:(BOOL)repeat block:(dispatch_block_t)block {
    return [self initWithTimeInterval:interval repeat:repeat queue:dispatch_get_main_queue() block:block];
}

- (instancetype)initWithTimeInterval:(NSTimeInterval)interval repeat:(BOOL)repeat queue:(dispatch_queue_t)queue block:(dispatch_block_t)block {
    NSAssert(queue != NULL, @"queue can't be NULL while create GCDTimer");
    
    if (self = [super init]) {
        // 創(chuàng)建線程
        _timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, queue);
        dispatch_source_set_timer(_timer, dispatch_time(DISPATCH_TIME_NOW, 0), interval * NSEC_PER_SEC, 0);
       
        // 執(zhí)行任務(wù)
        dispatch_source_set_event_handler(_timer, ^{
            if (block) {
                block();
            }
            if (!repeat) {
                self->_isFire = NO;
                dispatch_source_cancel(self->_timer);
            }
        });
        
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(interval * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            dispatch_resume(self->_timer);
            self->_isFire = YES;
        });
    }
    
    return self;
}

+ (instancetype)scheduledTimerWithTimeInterval:(NSTimeInterval)interval repeat:(BOOL)repeat queue:(dispatch_queue_t)queue block:(dispatch_block_t)block {
    return [[GCDTimer alloc] initWithTimeInterval:interval repeat:repeat queue:queue block:block];
}

+ (instancetype)scheduledTimerWithTimeInterval:(NSTimeInterval)interval repeat:(BOOL)repeat block:(dispatch_block_t)block {
    return [self scheduledTimerWithTimeInterval:interval repeat:repeat queue:dispatch_get_main_queue() block:block];
}

- (void)setTimeInterval:(NSTimeInterval)interval {
    if (_isFire) {
        dispatch_source_set_timer(_timer, dispatch_time(DISPATCH_TIME_NOW, 0), interval * NSEC_PER_SEC, 0);
    }
}

- (void)stop {
    if (_isFire) {
        _isFire = NO;
        //  掛起定時器隊列
        dispatch_suspend(_timer);
    }
}

- (void)restart {
    if (!_isFire) {
        _isFire = YES;
        // 恢復(fù)定時器隊列
        dispatch_resume(_timer);
    }
}

- (void)invalidate {
    _isFire = NO;
    // 取消定時器隊列
    dispatch_source_cancel(_timer);
}

- (void)dealloc {
    _isFire = NO;
    // 取消定時器隊列
    dispatch_source_cancel(_timer);
}

@end

4 Swift GCD定時器 封裝

import Foundation


class CGDTimer: NSObject {
    static let `default` = CGDTimer()
    
    private var timer: DispatchSourceTimer?
    
    deinit {
        timer?.cancel()
    }
    
    // 暫停定時器
    func stop() {
        timer?.suspend()
    }
    
    // 重啟定時器
    func restart() {
        timer?.resume()
    }
    
    // 清除定時器
    func invalidate() {
        timer?.cancel()
    }
    
    // 主線程設(shè)置CGD定時器
    // interval 時間
    // isRepeat 是否循環(huán)
    // block 執(zhí)行返回
    func setTimeInterval(interval: Int, isRepeat: Bool = false, block: @escaping  () -> Void) {
        // 默認在主隊列中調(diào)度使用
        timer = DispatchSource.makeTimerSource()
        
        var timeType: DispatchTimeInterval = .never
        if isRepeat {
            timeType = .seconds(interval)
        }
        timer?.schedule(deadline: DispatchTime.now(), repeating: timeType, leeway: .nanoseconds(1))
        
        timer?.setEventHandler {
            DispatchQueue.main.async {
                // 執(zhí)行任務(wù)
                block()
            }
        }
        
        timer?.setRegistrationHandler(handler: {
            DispatchQueue.main.async {
                // Timer開始工作了
            }
        })
//        timer?.activate()
        timer?.resume()
    }
    
    // 線程設(shè)置CGD定時器
    // ztimer 對應(yīng)線程
    // interval 時間
    // isRepeat 是否循環(huán)
    // block 執(zhí)行返回
    func setTimeInterval(ztimer: DispatchSourceTimer, interval: Int, isRepeat: Bool = false, block: @escaping  () -> Void) {
        timer = ztimer

        var timeType: DispatchTimeInterval = .never
        if isRepeat {
            timeType = .seconds(interval)
        }
        timer?.schedule(deadline: DispatchTime.now(), repeating: timeType, leeway: .nanoseconds(1))
        
        timer?.setEventHandler {
            DispatchQueue.main.async {
                // 執(zhí)行任務(wù)
                block()
            }
        }
        
        timer?.setRegistrationHandler(handler: {
            DispatchQueue.main.async {
                // Timer開始工作了
            }
        })
//        timer?.activate()
        timer?.resume()
    }
    
    
}

參考文章

https://blog.csdn.net/guoyongming925/article/details/110224064

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

推薦閱讀更多精彩內(nèi)容

  • 字符串 1.什么是字符串 使用單引號或者雙引號括起來的字符集就是字符串。 引號中單獨的符號、數(shù)字、字母等叫字符。 ...
    mango_2e17閱讀 7,546評論 1 7
  • 《閉上眼睛才能看清楚自己》這本書是香海禪寺主持賢宗法師的人生體悟,修行心得及講學(xué)錄,此書從六個章節(jié)講述了禪修是什么...
    宜均閱讀 10,140評論 1 25
  • 前言 Google Play應(yīng)用市場對于應(yīng)用的targetSdkVersion有了更為嚴格的要求。從 2018 年...
    申國駿閱讀 64,727評論 15 98
  • 第七章:理性的投資觀 字數(shù): 1.投資要圍繞目的進行 投資的目的是為了掙錢。投資的除了金錢還有時間和精力也是一種投...
    幸福萍寶閱讀 3,369評論 1 2
  • 本文轉(zhuǎn)載自微信公眾號“電子搬磚師”,原文鏈接 這篇文章會以特別形象通俗的方式講講什么是PID。 很多人看到網(wǎng)上寫的...
    這個飛宏不太冷閱讀 6,947評論 2 15