From 325399b8846c4b5a5bf7890c09c89154a10e9a0b Mon Sep 17 00:00:00 2001 From: Nathan Manceaux-Panot Date: Sat, 10 Jan 2026 14:42:05 +0100 Subject: [PATCH 1/2] Fix running many session duration timers at once If TelemetryDeck.initialize() is called while the app is in the background, then the SessionManager creates a new session update timer in startNewSession(), that is ultimately never invalidated. These runaway timers can add up, and consume significant resources. This commit prevents this from happening, by always checking for foreground state before creating the timer. --- Sources/TelemetryDeck/Helpers/SessionManager.swift | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/Sources/TelemetryDeck/Helpers/SessionManager.swift b/Sources/TelemetryDeck/Helpers/SessionManager.swift index 33d2427..5b65279 100644 --- a/Sources/TelemetryDeck/Helpers/SessionManager.swift +++ b/Sources/TelemetryDeck/Helpers/SessionManager.swift @@ -44,6 +44,8 @@ final class SessionManager: @unchecked Sendable { return encoder }() + private var appIsInForeground = true + private var recentSessions: [StoredSession] private var deletedSessionsCount: Int { @@ -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() @@ -201,12 +206,14 @@ 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, From ab774f3f14f395fe09d83052f1a99069da769712 Mon Sep 17 00:00:00 2001 From: Nathan Manceaux-Panot Date: Sat, 10 Jan 2026 15:17:37 +0100 Subject: [PATCH 2/2] Reduce session timer frequency Elapsed time is counted anyway, whenever updateSessionDuration() is called. This includes when switching away from the app. --- Sources/TelemetryDeck/Helpers/SessionManager.swift | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Sources/TelemetryDeck/Helpers/SessionManager.swift b/Sources/TelemetryDeck/Helpers/SessionManager.swift index 5b65279..119007c 100644 --- a/Sources/TelemetryDeck/Helpers/SessionManager.swift +++ b/Sources/TelemetryDeck/Helpers/SessionManager.swift @@ -160,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, @@ -216,7 +216,7 @@ final class SessionManager: @unchecked Sendable { appIsInForeground = true self.updateSessionDuration() self.sessionDurationUpdater = Timer.scheduledTimer( - timeInterval: 1, + timeInterval: 5, target: self, selector: #selector(updateSessionDuration), userInfo: nil,