Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Deactivate BG Tasks #257

Merged
merged 2 commits into from
Mar 17, 2022
Merged
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
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()
}
}