Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix for crash RepeatingTimer.timer.getter #70

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,29 @@ import Foundation
class RepeatingTimer {

static let shared = RepeatingTimer()
var timeInterval: TimeInterval = 0
private var timeInterval: TimeInterval = 0
private var newTimer: DispatchSourceTimer?

private init() { }

init(timeInterval: TimeInterval) {
self.timeInterval = timeInterval
}

func setup(timeInterval: TimeInterval) {
self.timeInterval = timeInterval
setupTimer()
}

private func setupTimer() {
guard timeInterval > 0 else { return }
newTimer = DispatchSource.makeTimerSource()
newTimer?.schedule(deadline: .now() + timeInterval, repeating: timeInterval)
newTimer?.setEventHandler { [weak self] in
self?.eventHandler?()
}
}

private lazy var timer: DispatchSourceTimer = { [weak self] in
let t = DispatchSource.makeTimerSource()
guard let checkedSelf = self else { return t }
Expand All @@ -44,8 +59,13 @@ class RepeatingTimer {
private var state: Atomic<State> = Clickstream.timerCrashFixFlag ? Atomic(.notInitialized) : Atomic(.suspended)

deinit {
timer.setEventHandler {}
timer.cancel()
if Clickstream.timerCrashFixFlag {
newTimer?.setEventHandler {}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please rename this variable whenever we promote this experiment.

newTimer?.cancel()
} else {
timer.setEventHandler {}
timer.cancel()
}
/*
If the timer is suspended, calling cancel without resuming
triggers a crash. This is documented here https://forums.developer.apple.com/thread/15902
Expand All @@ -68,7 +88,11 @@ class RepeatingTimer {
state.mutate { state in
state = .resumed
}
timer.resume()
if Clickstream.timerCrashFixFlag {
newTimer?.resume()
} else {
timer.resume()
}
}

func suspend() {
Expand All @@ -78,6 +102,11 @@ class RepeatingTimer {
state.mutate { state in
state = .suspended
}
timer.suspend()

if Clickstream.timerCrashFixFlag {
newTimer?.suspend()
} else {
timer.suspend()
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,9 @@ final class DefaultKeepAliveServiceWithSafeTimer: KeepAliveService {
@discardableResult
private func makeTimer() -> RepeatingTimer? {
if Clickstream.timerCrashFixFlag {
let timerDuration = duration*reachability.connectionRetryCoefficient
RepeatingTimer.shared.timeInterval = timerDuration
self.timer = RepeatingTimer.shared
let timerDuration = duration*reachability.connectionRetryCoefficient
timer?.setup(timeInterval: timerDuration)
timer?.eventHandler = { [weak self] in
guard let checkedSelf = self else { return }
checkedSelf.performQueue.async {
Expand Down
Loading