IOS-1671 Clean up code for starting/canceling background task. IOS-1671
authorDirk Zimmermann <dz@pep.security>
Tue, 25 Jun 2019 15:34:22 +0200
branchIOS-1671
changeset 92153831330befb2
parent 9214 33021d7ac448
child 9216 dbf206db5d3d
IOS-1671 Clean up code for starting/canceling background task.
pEpForiOS/AppDelegate.swift
     1.1 --- a/pEpForiOS/AppDelegate.swift	Tue Jun 25 14:49:59 2019 +0200
     1.2 +++ b/pEpForiOS/AppDelegate.swift	Tue Jun 25 15:34:22 2019 +0200
     1.3 @@ -84,6 +84,14 @@
     1.4          }
     1.5      }
     1.6  
     1.7 +    private func cancelBackgroundTask() {
     1.8 +        guard syncUserActionsAndCleanupbackgroundTaskId != UIBackgroundTaskIdentifier.invalid else {
     1.9 +            return
    1.10 +        }
    1.11 +        application.endBackgroundTask(syncUserActionsAndCleanupbackgroundTaskId)
    1.12 +        syncUserActionsAndCleanupbackgroundTaskId = UIBackgroundTaskIdentifier.invalid
    1.13 +    }
    1.14 +
    1.15      /// Signals all PEPSession users to stop using a session as soon as possible.
    1.16      /// ReplicationService will assure all local changes triggered by the user are synced to the server
    1.17      /// and call it's delegate (me) after the last sync operation has finished.
    1.18 @@ -97,18 +105,15 @@
    1.19          }
    1.20          syncUserActionsAndCleanupbackgroundTaskId =
    1.21              application.beginBackgroundTask(expirationHandler: { [unowned self] in
    1.22 -                Log.shared.warn(
    1.23 -                    "syncUserActionsAndCleanupbackgroundTask with ID %d expired",
    1.24 -                    self.syncUserActionsAndCleanupbackgroundTaskId.rawValue)
    1.25 +                let taskId = self.syncUserActionsAndCleanupbackgroundTaskId
    1.26 +
    1.27                  // We migh want to call some (yet unexisting) emergency shutdown on
    1.28                  // ReplicationService here that brutally shuts down everything.
    1.29 -                self.application.endBackgroundTask(
    1.30 -                    self.syncUserActionsAndCleanupbackgroundTaskId)
    1.31 -                self.syncUserActionsAndCleanupbackgroundTaskId = UIBackgroundTaskIdentifier.invalid
    1.32 +                self.cancelBackgroundTask()
    1.33  
    1.34                  Log.shared.errorAndCrash(
    1.35                      "syncUserActionsAndCleanupbackgroundTask with ID %d expired",
    1.36 -                    self.syncUserActionsAndCleanupbackgroundTaskId.rawValue)
    1.37 +                    taskId.rawValue)
    1.38              })
    1.39          messageModelService?.processAllUserActionsAndStop()
    1.40      }