From 6d90673b3ace6d97d256832fcca2b37a14e1c563 Mon Sep 17 00:00:00 2001 From: Stefan Ceriu Date: Fri, 10 Jan 2025 12:54:19 +0200 Subject: [PATCH] Simplify how to we handle background task expirations. --- .../Sources/Application/AppCoordinator.swift | 30 +++++++++---------- 1 file changed, 15 insertions(+), 15 deletions(-) diff --git a/ElementX/Sources/Application/AppCoordinator.swift b/ElementX/Sources/Application/AppCoordinator.swift index 1c040fb479..56cd4aa7f8 100644 --- a/ElementX/Sources/Application/AppCoordinator.swift +++ b/ElementX/Sources/Application/AppCoordinator.swift @@ -965,30 +965,30 @@ class AppCoordinator: AppCoordinatorProtocol, AuthenticationFlowCoordinatorDeleg return } - backgroundTask = appMediator.beginBackgroundTask { [weak self] in - guard let self else { return } - + backgroundTask = appMediator.beginBackgroundTask { MXLog.info("Background task is about to expire.") - stopSync(isBackgroundTask: true) { [weak self] in - guard let self, let backgroundTask else { return } - - MXLog.info("Ending background task.") - appMediator.endBackgroundTask(backgroundTask) - self.backgroundTask = nil + + self.stopSync(isBackgroundTask: true) { + self.endActiveBackgroundTask() } } } - + @objc private func applicationDidBecomeActive() { MXLog.info("Application did become active") - - if let backgroundTask { - appMediator.endBackgroundTask(backgroundTask) - self.backgroundTask = nil + endActiveBackgroundTask() + startSync() + } + + private func endActiveBackgroundTask() { + guard let backgroundTask else { + return } - startSync() + MXLog.info("Ending background task.") + appMediator.endBackgroundTask(backgroundTask) + self.backgroundTask = nil } // MARK: Background app refresh