diff --git a/Loop/AppDelegate.swift b/Loop/AppDelegate.swift index 33af2f4ae0..7a5c48a5e7 100644 --- a/Loop/AppDelegate.swift +++ b/Loop/AppDelegate.swift @@ -25,6 +25,7 @@ final class AppDelegate: UIResponder, UIApplicationDelegate { setenv("CFNETWORK_DIAGNOSTICS", "3", 1) self.launchOptions = launchOptions + loopAppManager.registerBackgroundTasks() return true } diff --git a/Loop/Managers/LoopAppManager.swift b/Loop/Managers/LoopAppManager.swift index 47f1b67d7a..2e9761470e 100644 --- a/Loop/Managers/LoopAppManager.swift +++ b/Loop/Managers/LoopAppManager.swift @@ -130,12 +130,16 @@ class LoopAppManager: NSObject { func registerBackgroundTasks() { let taskIdentifier = CriticalEventLogExportManager.historicalExportBackgroundTaskIdentifier - let registered = BGTaskScheduler.shared.register(forTaskWithIdentifier: taskIdentifier, using: nil) { task in - guard let criticalEventLogExportManager = self.criticalEventLogExportManager else { - self.log.error("Critical event log export launch handler called before initialization complete!") + let registered = BGTaskScheduler.shared.register(forTaskWithIdentifier: taskIdentifier, using: .main) { task in + guard let criticalEventLogExportManager = self.criticalEventLogExportManager, + let processingTask = task as? BGProcessingTask else { + self.log.error("Cannot start critical event log export: export manager or processing task unavailable") + task.setTaskCompleted(success: false) return } - criticalEventLogExportManager.handleCriticalEventLogHistoricalExportBackgroundTask(task as! BGProcessingTask) + DispatchQueue.global(qos: .background).async { + criticalEventLogExportManager.handleCriticalEventLogHistoricalExportBackgroundTask(processingTask) + } } if registered { log.debug("Critical event log export background task registered") @@ -149,8 +153,6 @@ class LoopAppManager: NSObject { UNUserNotificationCenter.current().delegate = self - registerBackgroundTasks() - Task { await resumeLaunch() }