File tree Expand file tree Collapse file tree 2 files changed +48
-17
lines changed
Sources/ClickstreamLib/NetworkManager/Infrastructure/Utilities Expand file tree Collapse file tree 2 files changed +48
-17
lines changed Original file line number Diff line number Diff line change @@ -13,8 +13,14 @@ import Foundation
13
13
/// crashes that occur from calling resume multiple times on a timer that is
14
14
/// already resumed (noted by https://github.com/SiftScience/sift-ios/issues/52)
15
15
class RepeatingTimer {
16
+
17
+ static let shared = RepeatingTimer ( )
18
+
19
+ var timeInterval : TimeInterval = 0
20
+
21
+ private var suspensionCount = 0
16
22
17
- let timeInterval : TimeInterval
23
+ private init ( ) { }
18
24
19
25
init ( timeInterval: TimeInterval ) {
20
26
self . timeInterval = timeInterval
@@ -41,33 +47,45 @@ class RepeatingTimer {
41
47
deinit {
42
48
timer. setEventHandler { }
43
49
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 ( )
51
55
eventHandler = nil
52
56
}
53
57
54
58
func resume( ) {
55
59
if state. value == . resumed {
56
60
return
57
61
}
62
+ suspensionCount -= 1
58
63
state. mutate { state in
59
64
state = . resumed
60
65
}
61
- timer. resume ( )
66
+ if Clickstream . timerCrashFixFlag {
67
+ if suspensionCount > 0 {
68
+ self . timer. resume ( )
69
+ }
70
+ } else {
71
+ timer. resume ( )
72
+ }
62
73
}
63
74
64
75
func suspend( ) {
65
76
if state. value == . suspended {
66
77
return
67
78
}
79
+ suspensionCount += 1
68
80
state. mutate { state in
69
81
state = . suspended
70
82
}
71
- timer. suspend ( )
83
+ if Clickstream . timerCrashFixFlag {
84
+ if suspensionCount > 0 {
85
+ timer. suspend ( )
86
+ }
87
+ } else {
88
+ timer. suspend ( )
89
+ }
72
90
}
73
91
}
Original file line number Diff line number Diff line change @@ -89,15 +89,28 @@ final class DefaultKeepAliveServiceWithSafeTimer: KeepAliveService {
89
89
90
90
@discardableResult
91
91
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
+ }
98
111
}
112
+ return timer
99
113
}
100
- return timer
101
114
}
102
115
103
116
func stop( ) {
You can’t perform that action at this time.
0 commit comments