Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Simplify how to we handle background task expirations. #3670

Merged
merged 2 commits into from
Jan 10, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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) {
stefanceriu marked this conversation as resolved.
Show resolved Hide resolved
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
Loading