Simplify how to we handle background task expirations. (#3670)

This commit is contained in:
Stefan Ceriu
2025-01-10 16:43:39 +02:00
committed by GitHub
parent fc719d9aa6
commit 2671d90bc1
@@ -965,16 +965,14 @@ class AppCoordinator: AppCoordinatorProtocol, AuthenticationFlowCoordinatorDeleg
return return
} }
backgroundTask = appMediator.beginBackgroundTask { [weak self] in backgroundTask = appMediator.beginBackgroundTask {
guard let self else { return }
MXLog.info("Background task is about to expire.") 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.") // We're intentionally strongly retaining self here to an EXC_BAD_ACCESS
appMediator.endBackgroundTask(backgroundTask) // `backgroundTask` will be eventually released in `endActiveBackgroundTask`
self.backgroundTask = nil // https://sentry.tools.element.io/organizations/element/issues/4477794/events/9cfd04e4d045440f87498809cf718de5/
self.stopSync(isBackgroundTask: true) {
self.endActiveBackgroundTask()
} }
} }
} }
@@ -982,13 +980,18 @@ class AppCoordinator: AppCoordinatorProtocol, AuthenticationFlowCoordinatorDeleg
@objc @objc
private func applicationDidBecomeActive() { private func applicationDidBecomeActive() {
MXLog.info("Application did become active") MXLog.info("Application did become active")
endActiveBackgroundTask()
startSync()
}
if let backgroundTask { private func endActiveBackgroundTask() {
appMediator.endBackgroundTask(backgroundTask) guard let backgroundTask else {
self.backgroundTask = nil return
} }
startSync() MXLog.info("Ending background task.")
appMediator.endBackgroundTask(backgroundTask)
self.backgroundTask = nil
} }
// MARK: Background app refresh // MARK: Background app refresh