Skip to content

Commit 468afd8

Browse files
authored
Make stopSync more aware of background usage. (#3501)
1 parent b9409e5 commit 468afd8

File tree

1 file changed

+11
-10
lines changed

1 file changed

+11
-10
lines changed

ElementX/Sources/Application/AppCoordinator.swift

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -561,7 +561,7 @@ class AppCoordinator: AppCoordinatorProtocol, AuthenticationFlowCoordinatorDeleg
561561

562562
showLoadingIndicator()
563563

564-
stopSync()
564+
stopSync(isBackgroundTask: false)
565565
userSessionFlowCoordinator?.stop()
566566

567567
guard !isSoft else {
@@ -748,7 +748,7 @@ class AppCoordinator: AppCoordinatorProtocol, AuthenticationFlowCoordinatorDeleg
748748

749749
navigationRootCoordinator.setRootCoordinator(PlaceholderScreenCoordinator())
750750

751-
stopSync()
751+
stopSync(isBackgroundTask: false)
752752
userSessionFlowCoordinator?.stop()
753753

754754
let userID = userSession.clientProxy.userID
@@ -848,7 +848,11 @@ class AppCoordinator: AppCoordinatorProtocol, AuthenticationFlowCoordinatorDeleg
848848

849849
// MARK: - Application State
850850

851-
private func stopSync() {
851+
private func stopSync(isBackgroundTask: Bool) {
852+
if isBackgroundTask, UIApplication.shared.applicationState == .active {
853+
// Attempt to stop the background task sync loop cleanly, only if the app not already running
854+
return
855+
}
852856
userSession?.clientProxy.stopSync()
853857
clientProxyObserver = nil
854858
}
@@ -911,7 +915,7 @@ class AppCoordinator: AppCoordinatorProtocol, AuthenticationFlowCoordinatorDeleg
911915

912916
@objc
913917
private func applicationWillTerminate() {
914-
stopSync()
918+
stopSync(isBackgroundTask: false)
915919
}
916920

917921
@objc
@@ -930,7 +934,7 @@ class AppCoordinator: AppCoordinatorProtocol, AuthenticationFlowCoordinatorDeleg
930934
backgroundTask = appMediator.beginBackgroundTask { [weak self] in
931935
guard let self else { return }
932936

933-
stopSync()
937+
stopSync(isBackgroundTask: true)
934938

935939
if let backgroundTask {
936940
appMediator.endBackgroundTask(backgroundTask)
@@ -988,10 +992,7 @@ class AppCoordinator: AppCoordinatorProtocol, AuthenticationFlowCoordinatorDeleg
988992
scheduleBackgroundAppRefresh()
989993

990994
task.expirationHandler = { [weak self] in
991-
if UIApplication.shared.applicationState != .active {
992-
// Attempt to stop the sync loop cleanly, only if the app not already running
993-
self?.stopSync()
994-
}
995+
self?.stopSync(isBackgroundTask: true)
995996

996997
MXLog.info("Background app refresh task expired")
997998
task.setTaskCompleted(success: true)
@@ -1015,7 +1016,7 @@ class AppCoordinator: AppCoordinatorProtocol, AuthenticationFlowCoordinatorDeleg
10151016

10161017
// Make sure we stop the sync loop, otherwise the ongoing request is immediately
10171018
// handled the next time the app refreshes, which can trigger timeout failures.
1018-
stopSync()
1019+
stopSync(isBackgroundTask: true)
10191020
backgroundRefreshSyncObserver?.cancel()
10201021

10211022
task.setTaskCompleted(success: true)

0 commit comments

Comments
 (0)