diff --git a/ElementX/Sources/Application/AppCoordinator.swift b/ElementX/Sources/Application/AppCoordinator.swift index eec3b77f9..012f248f1 100644 --- a/ElementX/Sources/Application/AppCoordinator.swift +++ b/ElementX/Sources/Application/AppCoordinator.swift @@ -26,8 +26,6 @@ class AppCoordinator: AppCoordinatorProtocol, AuthenticationFlowCoordinatorDeleg /// Common background task to continue long-running tasks in the background. private var backgroundTask: UIBackgroundTaskIdentifier? - - private var isSuspended = false private var userSession: UserSessionProtocol? { didSet { @@ -929,8 +927,6 @@ class AppCoordinator: AppCoordinatorProtocol, AuthenticationFlowCoordinatorDeleg } } - isSuspended = true - // This does seem to work if scheduled from the background task above // Schedule it here instead but with an earliest being date of 30 seconds scheduleBackgroundAppRefresh() @@ -944,12 +940,8 @@ class AppCoordinator: AppCoordinatorProtocol, AuthenticationFlowCoordinatorDeleg appMediator.endBackgroundTask(backgroundTask) self.backgroundTask = nil } - - if isSuspended { - startSync() - } - - isSuspended = false + + startSync() } // MARK: Background app refresh @@ -989,7 +981,11 @@ class AppCoordinator: AppCoordinatorProtocol, AuthenticationFlowCoordinatorDeleg scheduleBackgroundAppRefresh() task.expirationHandler = { [weak self] in - self?.stopSync() // Attempt to stop the sync loop cleanly. + if UIApplication.shared.applicationState != .active { + // Attempt to stop the sync loop cleanly, only if the app not already running + self?.stopSync() + } + MXLog.info("Background app refresh task expired") task.setTaskCompleted(success: true) }