Skip to content

Commit

Permalink
Dispatch timer updates to main queue (#6)
Browse files Browse the repository at this point in the history
  • Loading branch information
kylebshr authored Feb 17, 2023
1 parent 85fffc7 commit 22878e4
Showing 1 changed file with 16 additions and 10 deletions.
26 changes: 16 additions & 10 deletions Sources/SegmentAmplitude/AmplitudeSession.swift
Original file line number Diff line number Diff line change
Expand Up @@ -148,20 +148,26 @@ extension AmplitudeSession {
}

func startTimer() {
sessionTimer?.invalidate()
sessionTimer = Timer(timeInterval: fireTime, target: self,
selector: #selector(handleTimerFire(_:)),
userInfo: nil, repeats: true)
sessionTimer?.tolerance = 0.3
if let sessionTimer = sessionTimer {
// Use the RunLoop current to avoid retaining self
RunLoop.current.add(sessionTimer, forMode: .common)
// Starting and stopping the timer has to be done from the same thread; always dispatch to main queue
DispatchQueue.main.async {
self.sessionTimer?.invalidate()
self.sessionTimer = Timer(timeInterval: self.fireTime, target: self,
selector: #selector(self.handleTimerFire(_:)),
userInfo: nil, repeats: true)
self.sessionTimer?.tolerance = 0.3
if let sessionTimer = self.sessionTimer {
// Use the RunLoop current to avoid retaining self
RunLoop.current.add(sessionTimer, forMode: .common)
}
}
}

func stopTimer() {
sessionTimer?.invalidate()
sessionID = -1
// Starting and stopping the timer has to be done from the same thread; always dispatch to main queue
DispatchQueue.main.async {
self.sessionTimer?.invalidate()
self.sessionID = -1
}
}
}

Expand Down

0 comments on commit 22878e4

Please # to comment.