From bd10796ea194f9a51f636670c3f935ec6d7348c1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dennis=20Ju=CC=88ni?= Date: Thu, 17 Mar 2022 15:07:03 +0100 Subject: [PATCH 1/2] explicitly cancel BGTasks --- .../DP3TBackgroundTaskManager.swift | 24 ++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) diff --git a/Sources/DP3TSDK/Background/DP3TBackgroundTaskManager.swift b/Sources/DP3TSDK/Background/DP3TBackgroundTaskManager.swift index 56f8ebab..61aca242 100644 --- a/Sources/DP3TSDK/Background/DP3TBackgroundTaskManager.swift +++ b/Sources/DP3TSDK/Background/DP3TBackgroundTaskManager.swift @@ -32,7 +32,15 @@ class DP3TBackgroundTaskManager { private weak var tracer: Tracer! - var shouldReschedule: Bool = true + var shouldReschedule: Bool = true { + didSet { + if shouldReschedule == false { + if #available(iOS 13.0, *) { + cancelScheduledTask() + } + } + } + } init(handler: DP3TBackgroundHandler?, keyProvider: DiagnosisKeysProvider, @@ -87,6 +95,11 @@ class DP3TBackgroundTaskManager { private func handleiOS12BackgroundLaunch() { logger.trace() + + if !shouldReschedule { + return + } + let queue = OperationQueue() if let handler = handler { @@ -110,6 +123,10 @@ class DP3TBackgroundTaskManager { private func handleExposureNotificationBackgroundTask(_ task: BGTask) { logger.trace() scheduleBackgroundTasks() + + if !shouldReschedule { + return + } let queue = OperationQueue() @@ -213,4 +230,9 @@ class DP3TBackgroundTaskManager { logger.error("Exposure notification task schedule failed error: %{public}@", error.localizedDescription) } } + + @available(iOS 13.0, *) + func cancelScheduledTask() { + BGTaskScheduler.shared.cancelAllTaskRequests() + } } From 744867d4d94d7537fd17753c285ac11f1eddb61c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dennis=20Ju=CC=88ni?= Date: Thu, 17 Mar 2022 15:18:32 +0100 Subject: [PATCH 2/2] add check to RefreshTask --- Sources/DP3TSDK/Background/DP3TBackgroundTaskManager.swift | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/Sources/DP3TSDK/Background/DP3TBackgroundTaskManager.swift b/Sources/DP3TSDK/Background/DP3TBackgroundTaskManager.swift index 61aca242..2e137805 100644 --- a/Sources/DP3TSDK/Background/DP3TBackgroundTaskManager.swift +++ b/Sources/DP3TSDK/Background/DP3TBackgroundTaskManager.swift @@ -171,6 +171,10 @@ class DP3TBackgroundTaskManager { private func handleRefreshTask(_ task: BGTask) { logger.trace() scheduleBackgroundTasks() + + if !shouldReschedule { + return + } let queue = OperationQueue() let completionGroup = DispatchGroup()