開發(fā)過(guò)程中,我們可能會(huì)經(jīng)常使用到計(jì)時(shí)器。蘋果為我們提供了Timer。但是在平時(shí)使用過(guò)程中會(huì)發(fā)現(xiàn)使用Timer會(huì)有許多的不便
1:必須保證在一個(gè)活躍的runloop,我們知道主線程的runloop是活躍的,但是在其他異步線程runloop就需要我們自己去開啟,非常麻煩。
2:Timer的創(chuàng)建和銷毀必須在同一個(gè)線程。跨線程就操作不了
3:內(nèi)存問(wèn)題。可能循環(huán)引用造成內(nèi)存泄露
由于存在上述問(wèn)題,我們可以采用GCD封裝來(lái)解決。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
|
import UIKit typealias ActionBlock = () -> () class MRGCDTimer: NSObject { static let share = MRGCDTimer() lazy var timerContainer = [String : DispatchSourceTimer]() /// 創(chuàng)建一個(gè)名字為name的定時(shí) /// /// - Parameters: /// - name: 定時(shí)器的名字 /// - timeInterval: 時(shí)間間隔 /// - queue: 線程 /// - repeats: 是否重復(fù) /// - action: 執(zhí)行的操作 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的計(jì)時(shí)器 /// /// - Parameter name: 計(jì)時(shí)器的名字 func destoryTimer(withName name:String?) { let timer = timerContainer[name!] if timer == nil { return } timerContainer.removeValue(forKey: name!) timer?.cancel() } /// 檢測(cè)是否已經(jīng)存在名字為name的計(jì)時(shí)器 /// /// - Parameter name: 計(jì)時(shí)器的名字 /// - Returns: 返回bool值 func isExistTimer(withName name:String?) -> Bool { if timerContainer[name!] != nil { return true } return false } } |
使用方法
1
2
3
4
|
MRGCDTimer.share.scheduledDispatchTimer(withName: "name" , timeInterval: 1, queue: .main, repeats: true ) { //code self.updateCounter() } |
取消計(jì)時(shí)器
1
|
MRGCDTimer.share.destoryTimer(withName: "name" ) |
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持服務(wù)器之家。
原文鏈接:https://blog.csdn.net/weixin_42779997/article/details/88173588