Prevent the background task's expiration from stopping the main sync loop.

This commit is contained in:
Stefan Ceriu
2024-11-06 12:41:02 +02:00
parent 8de44b95bf
commit b9d05fe7f3
@@ -26,8 +26,6 @@ class AppCoordinator: AppCoordinatorProtocol, AuthenticationFlowCoordinatorDeleg
/// Common background task to continue long-running tasks in the background. /// Common background task to continue long-running tasks in the background.
private var backgroundTask: UIBackgroundTaskIdentifier? private var backgroundTask: UIBackgroundTaskIdentifier?
private var isSuspended = false
private var userSession: UserSessionProtocol? { private var userSession: UserSessionProtocol? {
didSet { didSet {
@@ -929,8 +927,6 @@ class AppCoordinator: AppCoordinatorProtocol, AuthenticationFlowCoordinatorDeleg
} }
} }
isSuspended = true
// This does seem to work if scheduled from the background task above // 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 // Schedule it here instead but with an earliest being date of 30 seconds
scheduleBackgroundAppRefresh() scheduleBackgroundAppRefresh()
@@ -944,12 +940,8 @@ class AppCoordinator: AppCoordinatorProtocol, AuthenticationFlowCoordinatorDeleg
appMediator.endBackgroundTask(backgroundTask) appMediator.endBackgroundTask(backgroundTask)
self.backgroundTask = nil self.backgroundTask = nil
} }
if isSuspended { startSync()
startSync()
}
isSuspended = false
} }
// MARK: Background app refresh // MARK: Background app refresh
@@ -989,7 +981,11 @@ class AppCoordinator: AppCoordinatorProtocol, AuthenticationFlowCoordinatorDeleg
scheduleBackgroundAppRefresh() scheduleBackgroundAppRefresh()
task.expirationHandler = { [weak self] in 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") MXLog.info("Background app refresh task expired")
task.setTaskCompleted(success: true) task.setTaskCompleted(success: true)
} }