Skip to content

Commit

Permalink
Simplify how to we handle background task expirations. (#3670)
Browse files Browse the repository at this point in the history
  • Loading branch information
stefanceriu authored Jan 10, 2025
1 parent fc719d9 commit 2671d90
Showing 1 changed file with 18 additions and 15 deletions.
33 changes: 18 additions & 15 deletions ElementX/Sources/Application/AppCoordinator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 2671d90

Please sign in to comment.