Skip to content

Commit fe31755

Browse files
committed
fix: add deinit for RETimer
1 parent b6d6228 commit fe31755

File tree

2 files changed

+12
-4
lines changed

2 files changed

+12
-4
lines changed

Example/iOS_Example/iOS_Example/RETimerViewController.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -145,20 +145,20 @@ class RETimerViewController: UIViewController {
145145

146146
func startTimer2() {
147147
timeLabel2.textColor = .blue
148-
reTimer2 = RETimer.scheduledTimer(timeInterval: 0.001) { timer in
149-
self.timeLabel2.text = Date().re.string(withFormat: "HH:mm:ss.SSS")
148+
reTimer2 = RETimer.scheduledTimer(timeInterval: 0.001) { [weak self] timer in
149+
self?.timeLabel2.text = Date().re.string(withFormat: "HH:mm:ss.SSS")
150150
}
151151
}
152152

153153
func startTimer3() {
154154
timeLabel3.textColor = .blue
155-
reTimer3 = RETimer.scheduledTimer(timeInterval: 0.001) { timer in
155+
reTimer3 = RETimer.scheduledTimer(timeInterval: 0.001) { [weak self] timer in
156156
let minutes = Int(timer.totalElapsedTime / 60.0)
157157
let seconds = Int(timer.totalElapsedTime - minutes.re.double * 60.0)
158158
let minutesInMilliseconds = minutes.re.double * 60 * 1000
159159
let secondsInMilliseconds = seconds.re.double * 1000
160160
let milliseconds = Int(timer.totalElapsedTime * 1000 - minutesInMilliseconds - secondsInMilliseconds)
161-
self.timeLabel3.text = String(format: "%02d:%02d.%03d", minutes, seconds, milliseconds)
161+
self?.timeLabel3.text = String(format: "%02d:%02d.%03d", minutes, seconds, milliseconds)
162162
}
163163
}
164164

Sources/ReerKit/Utility/RETimer.swift

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -232,6 +232,14 @@ public final class RETimer {
232232
timer.cancel()
233233
state = .invalidated
234234
}
235+
236+
deinit {
237+
timer.setEventHandler(handler: nil)
238+
if state == .suspended {
239+
timer.resume()
240+
}
241+
timer.cancel()
242+
}
235243
}
236244

237245
public extension RETimer {

0 commit comments

Comments
 (0)