Skip to content

Instantly share code, notes, and snippets.

@danielgalasko
Last active March 28, 2024 10:26
Show Gist options
  • Save danielgalasko/1da90276f23ea24cb3467c33d2c05768 to your computer and use it in GitHub Desktop.
Save danielgalasko/1da90276f23ea24cb3467c33d2c05768 to your computer and use it in GitHub Desktop.
A repeating GCD timer that can run on a background queue
/// RepeatingTimer mimics the API of DispatchSourceTimer but in a way that prevents
/// crashes that occur from calling resume multiple times on a timer that is
/// already resumed (noted by https://github.com/SiftScience/sift-ios/issues/52
class RepeatingTimer {
let timeInterval: TimeInterval
init(timeInterval: TimeInterval) {
self.timeInterval = timeInterval
}
private lazy var timer: DispatchSourceTimer = {
let t = DispatchSource.makeTimerSource()
t.schedule(deadline: .now() + self.timeInterval, repeating: self.timeInterval)
t.setEventHandler(handler: { [weak self] in
self?.eventHandler?()
})
return t
}()
var eventHandler: (() -> Void)?
private enum State {
case suspended
case resumed
}
private var state: State = .suspended
deinit {
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
*/
resume()
eventHandler = nil
}
func resume() {
if state == .resumed {
return
}
state = .resumed
timer.resume()
}
func suspend() {
if state == .suspended {
return
}
state = .suspended
timer.suspend()
}
}
@iSachdeva
Copy link

iSachdeva commented Mar 2, 2020

This is not working when app goes in background. Any idea on resolution? Please help.

@Streebor
Copy link

This is not working when app goes in background. Any idea on resolution? Please help.

As far as I know, timers do not work in the background. What do you user your timer for?

If you have a stopwatch type of UI where your time needs to update when user reopens the app, try saving a timestamp when the app is backgrounded. Then, when app is foregrounded again by the user, calculate the diff in time and update your UI and restart your timer.

@ThushalMadu
Copy link

@iSachdeva & @Streebor, any solution for run a background timer?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment