diff --git a/Sources/DP3TSDK/Background/DP3TBackgroundTaskManager.swift b/Sources/DP3TSDK/Background/DP3TBackgroundTaskManager.swift index 56f8ebab..2e137805 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() @@ -154,6 +171,10 @@ class DP3TBackgroundTaskManager { private func handleRefreshTask(_ task: BGTask) { logger.trace() scheduleBackgroundTasks() + + if !shouldReschedule { + return + } let queue = OperationQueue() let completionGroup = DispatchGroup() @@ -213,4 +234,9 @@ class DP3TBackgroundTaskManager { logger.error("Exposure notification task schedule failed error: %{public}@", error.localizedDescription) } } + + @available(iOS 13.0, *) + func cancelScheduledTask() { + BGTaskScheduler.shared.cancelAllTaskRequests() + } }