From 2671d90bc158eead70dc4036ba96504574a35276 Mon Sep 17 00:00:00 2001 From: Stefan Ceriu Date: Fri, 10 Jan 2025 16:43:39 +0200 Subject: [PATCH] Simplify how to we handle background task expirations. (#3670) --- .../Sources/Application/AppCoordinator.swift | 33 ++++++++++--------- 1 file changed, 18 insertions(+), 15 deletions(-) diff --git a/ElementX/Sources/Application/AppCoordinator.swift b/ElementX/Sources/Application/AppCoordinator.swift index 1c040fb479..8fb7f1bb48 100644 --- a/ElementX/Sources/Application/AppCoordinator.swift +++ b/ElementX/Sources/Application/AppCoordinator.swift @@ -965,30 +965,33 @@ 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 + + // We're intentionally strongly retaining self here to an EXC_BAD_ACCESS + // `backgroundTask` will be eventually released in `endActiveBackgroundTask` + // https://sentry.tools.element.io/organizations/element/issues/4477794/events/9cfd04e4d045440f87498809cf718de5/ + 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