Skip to content
Open
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
11 changes: 9 additions & 2 deletions Sources/TelemetryDeck/Helpers/SessionManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ final class SessionManager: @unchecked Sendable {
return encoder
}()

private var appIsInForeground = true

private var recentSessions: [StoredSession]

private var deletedSessionsCount: Int {
Expand Down Expand Up @@ -132,6 +134,9 @@ final class SessionManager: @unchecked Sendable {
}

func startNewSession() {
// Prevent incorrectly starting new session (and persistence timer) if in background
guard appIsInForeground else { return }

// stop automatic duration counting of previous session
self.stopSessionTimer()

Expand All @@ -155,7 +160,7 @@ final class SessionManager: @unchecked Sendable {
// start automatic duration counting of new session
self.updateSessionDuration()
self.sessionDurationUpdater = Timer.scheduledTimer(
timeInterval: 1,
timeInterval: 5,
target: self,
selector: #selector(updateSessionDuration),
userInfo: nil,
Expand Down Expand Up @@ -201,15 +206,17 @@ final class SessionManager: @unchecked Sendable {

@objc
private func handleDidEnterBackgroundNotification() {
appIsInForeground = false
self.updateSessionDuration()
self.stopSessionTimer()
}

@objc
private func handleWillEnterForegroundNotification() {
appIsInForeground = true
self.updateSessionDuration()
self.sessionDurationUpdater = Timer.scheduledTimer(
timeInterval: 1,
timeInterval: 5,
target: self,
selector: #selector(updateSessionDuration),
userInfo: nil,
Expand Down
Loading