Skip to content

Commit fd98f3a

Browse files
committed
fixing the crash on repeating timer
1 parent 5f1c017 commit fd98f3a

File tree

2 files changed

+48
-17
lines changed

2 files changed

+48
-17
lines changed

Sources/ClickstreamLib/NetworkManager/Infrastructure/Utilities/DispatchSourceTimer+RepeatingTimer.swift

Lines changed: 28 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,14 @@ import Foundation
1313
/// crashes that occur from calling resume multiple times on a timer that is
1414
/// already resumed (noted by https://github.com/SiftScience/sift-ios/issues/52)
1515
class RepeatingTimer {
16+
17+
static let shared = RepeatingTimer()
18+
19+
var timeInterval: TimeInterval = 0
20+
21+
private var suspensionCount = 0
1622

17-
let timeInterval: TimeInterval
23+
private init() { }
1824

1925
init(timeInterval: TimeInterval) {
2026
self.timeInterval = timeInterval
@@ -41,33 +47,45 @@ class RepeatingTimer {
4147
deinit {
4248
timer.setEventHandler {}
4349
timer.cancel()
44-
if !Clickstream.timerCrashFixFlag {
45-
/*
46-
If the timer is suspended, calling cancel without resuming
47-
triggers a crash. This is documented here https://forums.developer.apple.com/thread/15902
48-
*/
49-
resume()
50-
}
50+
/*
51+
If the timer is suspended, calling cancel without resuming
52+
triggers a crash. This is documented here https://forums.developer.apple.com/thread/15902
53+
*/
54+
resume()
5155
eventHandler = nil
5256
}
5357

5458
func resume() {
5559
if state.value == .resumed {
5660
return
5761
}
62+
suspensionCount -= 1
5863
state.mutate { state in
5964
state = .resumed
6065
}
61-
timer.resume()
66+
if Clickstream.timerCrashFixFlag {
67+
if suspensionCount > 0 {
68+
self.timer.resume()
69+
}
70+
} else {
71+
timer.resume()
72+
}
6273
}
6374

6475
func suspend() {
6576
if state.value == .suspended {
6677
return
6778
}
79+
suspensionCount += 1
6880
state.mutate { state in
6981
state = .suspended
7082
}
71-
timer.suspend()
83+
if Clickstream.timerCrashFixFlag {
84+
if suspensionCount > 0 {
85+
timer.suspend()
86+
}
87+
} else {
88+
timer.suspend()
89+
}
7290
}
7391
}

Sources/ClickstreamLib/NetworkManager/Infrastructure/Utilities/KeepAliveService.swift

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -89,15 +89,28 @@ final class DefaultKeepAliveServiceWithSafeTimer: KeepAliveService {
8989

9090
@discardableResult
9191
private func makeTimer() -> RepeatingTimer? {
92-
let timerDuration = duration*reachability.connectionRetryCoefficient
93-
timer = RepeatingTimer(timeInterval: timerDuration)
94-
timer?.eventHandler = { [weak self] in
95-
guard let checkedSelf = self else { return }
96-
checkedSelf.performQueue.async {
97-
checkedSelf.subscriber?()
92+
if Clickstream.timerCrashFixFlag {
93+
let timerDuration = duration*reachability.connectionRetryCoefficient
94+
RepeatingTimer.shared.timeInterval = timerDuration
95+
self.timer = RepeatingTimer.shared
96+
timer?.eventHandler = { [weak self] in
97+
guard let checkedSelf = self else { return }
98+
checkedSelf.performQueue.async {
99+
checkedSelf.subscriber?()
100+
}
101+
}
102+
return timer
103+
} else {
104+
let timerDuration = duration*reachability.connectionRetryCoefficient
105+
self.timer = RepeatingTimer(timeInterval: timerDuration)
106+
timer?.eventHandler = { [weak self] in
107+
guard let checkedSelf = self else { return }
108+
checkedSelf.performQueue.async {
109+
checkedSelf.subscriber?()
110+
}
98111
}
112+
return timer
99113
}
100-
return timer
101114
}
102115

103116
func stop() {

0 commit comments

Comments
 (0)