From 268f9bb65535734017f9ffc99e69952ea3a9623e Mon Sep 17 00:00:00 2001 From: Rishav Gupta Date: Mon, 8 Apr 2024 14:26:05 +0530 Subject: [PATCH] adding logic to add notInitialized as it would prevent state to first resume. --- .../DispatchSourceTimer+RepeatingTimer.swift | 41 ++++++++----------- .../Utilities/KeepAliveService.swift | 12 +++++- 2 files changed, 28 insertions(+), 25 deletions(-) diff --git a/Sources/ClickstreamLib/NetworkManager/Infrastructure/Utilities/DispatchSourceTimer+RepeatingTimer.swift b/Sources/ClickstreamLib/NetworkManager/Infrastructure/Utilities/DispatchSourceTimer+RepeatingTimer.swift index 38e5ef6..19f7727 100644 --- a/Sources/ClickstreamLib/NetworkManager/Infrastructure/Utilities/DispatchSourceTimer+RepeatingTimer.swift +++ b/Sources/ClickstreamLib/NetworkManager/Infrastructure/Utilities/DispatchSourceTimer+RepeatingTimer.swift @@ -18,17 +18,16 @@ class RepeatingTimer { var timeInterval: TimeInterval = 0 - private var suspensionCount = 0 - private init() { } init(timeInterval: TimeInterval) { self.timeInterval = timeInterval } - private lazy var timer: DispatchSourceTimer = { + private lazy var timer: DispatchSourceTimer = { [weak self] in let t = DispatchSource.makeTimerSource() - t.schedule(deadline: .now() + self.timeInterval, repeating: self.timeInterval) + guard let checkedSelf = self else { return t } + t.schedule(deadline: .now() + (checkedSelf.timeInterval), repeating: checkedSelf.timeInterval) t.setEventHandler(handler: { [weak self] in self?.eventHandler?() }) @@ -38,11 +37,12 @@ class RepeatingTimer { var eventHandler: (() -> Void)? private enum State { + case notInitialized case suspended case resumed } - private var state: Atomic = Atomic(.suspended) + private var state: Atomic = Clickstream.timerCrashFixFlag ? Atomic(.notInitialized) : Atomic(.suspended) deinit { timer.setEventHandler {} @@ -55,37 +55,32 @@ class RepeatingTimer { eventHandler = nil } + // decrements an internal suspension count. func resume() { - if state.value == .resumed { - return - } - suspensionCount -= 1 - state.mutate { state in - state = .resumed - } if Clickstream.timerCrashFixFlag { - if suspensionCount > 0 { - self.timer.resume() + if state.value == .resumed || state.value == .notInitialized { + return } } else { - timer.resume() + if state.value == .resumed { + return + } } + state.mutate { state in + state = .resumed + } + timer.resume() + } + // increments an internal suspension count. func suspend() { if state.value == .suspended { return } - suspensionCount += 1 state.mutate { state in state = .suspended } - if Clickstream.timerCrashFixFlag { - if suspensionCount > 0 { - timer.suspend() - } - } else { - timer.suspend() - } + timer.suspend() } } diff --git a/Sources/ClickstreamLib/NetworkManager/Infrastructure/Utilities/KeepAliveService.swift b/Sources/ClickstreamLib/NetworkManager/Infrastructure/Utilities/KeepAliveService.swift index 5152322..d242cf4 100644 --- a/Sources/ClickstreamLib/NetworkManager/Infrastructure/Utilities/KeepAliveService.swift +++ b/Sources/ClickstreamLib/NetworkManager/Infrastructure/Utilities/KeepAliveService.swift @@ -83,7 +83,11 @@ final class DefaultKeepAliveServiceWithSafeTimer: KeepAliveService { func start(with subscriber: @escaping KeepAliveCallback) { stop() - timer?.resume() + if Clickstream.timerCrashFixFlag { + timer?.suspend() + } else { + timer?.resume() + } self.subscriber = subscriber } @@ -114,6 +118,10 @@ final class DefaultKeepAliveServiceWithSafeTimer: KeepAliveService { } func stop() { - timer?.suspend() + if Clickstream.timerCrashFixFlag { + timer?.resume() + } else { + timer?.suspend() + } } }