Skip to content

Commit

Permalink
Merge pull request #257 from DP-3T/feature/deactivate-bg-task
Browse files Browse the repository at this point in the history
Deactivate BG Tasks
  • Loading branch information
dennisjueni authored Mar 17, 2022
2 parents 69d3e78 + 744867d commit 002a66e
Showing 1 changed file with 27 additions and 1 deletion.
28 changes: 27 additions & 1 deletion Sources/DP3TSDK/Background/DP3TBackgroundTaskManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -87,6 +95,11 @@ class DP3TBackgroundTaskManager {

private func handleiOS12BackgroundLaunch() {
logger.trace()

if !shouldReschedule {
return
}

let queue = OperationQueue()

if let handler = handler {
Expand All @@ -110,6 +123,10 @@ class DP3TBackgroundTaskManager {
private func handleExposureNotificationBackgroundTask(_ task: BGTask) {
logger.trace()
scheduleBackgroundTasks()

if !shouldReschedule {
return
}

let queue = OperationQueue()

Expand Down Expand Up @@ -154,6 +171,10 @@ class DP3TBackgroundTaskManager {
private func handleRefreshTask(_ task: BGTask) {
logger.trace()
scheduleBackgroundTasks()

if !shouldReschedule {
return
}

let queue = OperationQueue()
let completionGroup = DispatchGroup()
Expand Down Expand Up @@ -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()
}
}

0 comments on commit 002a66e

Please sign in to comment.