Skip to content

Commit

Permalink
Improve BackgroundTimer code
Browse files Browse the repository at this point in the history
  • Loading branch information
mvasilak committed Jan 29, 2025
1 parent 4a9d3b2 commit 287b66f
Showing 1 changed file with 9 additions and 13 deletions.
22 changes: 9 additions & 13 deletions Zotero/Controllers/BackgroundTimer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,15 @@ final class BackgroundTimer {
}

deinit {
invalidate()
guard let timer else { return }
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() {
Expand All @@ -47,18 +55,6 @@ final class BackgroundTimer {
timer.suspend()
}

func invalidate() {
guard let timer else { return }
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
}

private func createTimer() -> DispatchSourceTimer {
let timer = DispatchSource.makeTimerSource(flags: [], queue: queue)
let now = DispatchTime.now()
Expand Down

0 comments on commit 287b66f

Please sign in to comment.