Cleanup the background task that pauses the SDK when resigning active (#903)

* checking if the stop taskHandle is finished before stopping the bg task

* stopping the sync in background now works properly

* this will prevent the case in which if the app suspends and then is immediately foregrounded again, we do not start the sync

* now works as intended

* code improvement

* improved the code a bit
This commit is contained in:
Mauro
2023-05-19 10:28:30 +02:00
committed by GitHub
parent 03dece7bf4
commit 4e46c01945
5 changed files with 46 additions and 16 deletions
@@ -348,7 +348,7 @@ class AppCoordinator: AppCoordinatorProtocol, AuthenticationCoordinatorDelegate,
hideLoadingIndicator()
}
userSession.clientProxy.stopSync()
stopSync()
userSessionFlowCoordinator?.stop()
guard !isSoft else {
@@ -462,7 +462,7 @@ class AppCoordinator: AppCoordinatorProtocol, AuthenticationCoordinatorDelegate,
navigationRootCoordinator.setRootCoordinator(SplashScreenCoordinator())
userSession.clientProxy.stopSync()
stopSync()
userSessionFlowCoordinator?.stop()
let userID = userSession.userID
@@ -536,20 +536,24 @@ class AppCoordinator: AppCoordinatorProtocol, AuthenticationCoordinatorDelegate,
@objc
private func applicationWillResignActive() {
MXLog.info("Application will resign active")
guard backgroundTask == nil else {
return
}
backgroundTask = backgroundTaskService.startBackgroundTask(withName: "SuspendApp: \(UUID().uuidString)") { [weak self] in
guard let self else { return }
stopSync()
backgroundTask = nil
isSuspended = true
guard let self else {
return
}
userSession?.clientProxy.stopSync {
// No need to weakify self, this is a non escaping closure
self.backgroundTask?.stop()
self.backgroundTask = nil
}
}
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()
@@ -562,10 +566,11 @@ class AppCoordinator: AppCoordinatorProtocol, AuthenticationCoordinatorDelegate,
backgroundTask?.stop()
backgroundTask = nil
if isSuspended {
isSuspended = false
if isSuspended, userSession?.clientProxy.isSyncing == false {
startSync()
}
isSuspended = false
}
// MARK: Background app refresh