From ebed17ed2f7d2949733fba2dc1e66e2e518154d7 Mon Sep 17 00:00:00 2001 From: Maurice Parker Date: Thu, 31 Oct 2019 19:20:52 -0500 Subject: [PATCH] Tell iOS to wait while we are processing to allow us to try to finish --- iOS/AppDelegate.swift | 86 ++++++++++++++++++++++++++++++++----------- 1 file changed, 65 insertions(+), 21 deletions(-) diff --git a/iOS/AppDelegate.swift b/iOS/AppDelegate.swift index 6d2e2f292..be4c90b19 100644 --- a/iOS/AppDelegate.swift +++ b/iOS/AppDelegate.swift @@ -18,6 +18,7 @@ var appDelegate: AppDelegate! @UIApplicationMain class AppDelegate: UIResponder, UIApplicationDelegate, UNUserNotificationCenterDelegate, UnreadCountProvider { + private var waitBackgroundUpdateTask = UIBackgroundTaskIdentifier.invalid private var syncBackgroundUpdateTask = UIBackgroundTaskIdentifier.invalid var syncTimer: ArticleStatusSyncTimer? @@ -130,28 +131,9 @@ class AppDelegate: UIResponder, UIApplicationDelegate, UNUserNotificationCenterD func prepareAccountsForBackground() { syncTimer?.invalidate() - - // Schedule background app refresh scheduleBackgroundFeedRefresh() - - // Sync article status - let completeProcessing = { [unowned self] in - UIApplication.shared.endBackgroundTask(self.syncBackgroundUpdateTask) - self.syncBackgroundUpdateTask = UIBackgroundTaskIdentifier.invalid - } - - DispatchQueue.global(qos: .background).async { - self.syncBackgroundUpdateTask = UIApplication.shared.beginBackgroundTask { - completeProcessing() - os_log("Accounts sync processing terminated for running too long.", log: self.log, type: .info) - } - - DispatchQueue.main.async { - AccountManager.shared.syncArticleStatusAll() { - completeProcessing() - } - } - } + waitForProgressToFinish() + syncArticleStatus() } func prepareAccountsForForeground() { @@ -256,6 +238,68 @@ private extension AppDelegate { } +// MARK: Go To Background +private extension AppDelegate { + + func waitForProgressToFinish() { + let completeProcessing = { [unowned self] in + AccountManager.shared.saveAll() + UIApplication.shared.endBackgroundTask(self.waitBackgroundUpdateTask) + self.waitBackgroundUpdateTask = UIBackgroundTaskIdentifier.invalid + } + + self.waitBackgroundUpdateTask = UIApplication.shared.beginBackgroundTask { + completeProcessing() + os_log("Accounts wait for progress terminated for running too long.", log: self.log, type: .info) + } + + DispatchQueue.main.async { [weak self] in + self?.waitToComplete() { + completeProcessing() + } + } + } + + func waitToComplete(completion: @escaping () -> Void) { + guard UIApplication.shared.applicationState != .active else { + os_log("App came back to forground, no longer waiting.", log: self.log, type: .info) + completion() + return + } + + if AccountManager.shared.refreshInProgress { + os_log("Waiting for refresh progress to finish...", log: self.log, type: .info) + DispatchQueue.main.asyncAfter(deadline: .now() + 1.0) { [weak self] in + self?.waitToComplete() { + completion() + } + } + } else { + os_log("Refresh progress complete.", log: self.log, type: .info) + completion() + } + } + + func syncArticleStatus() { + let completeProcessing = { [unowned self] in + UIApplication.shared.endBackgroundTask(self.syncBackgroundUpdateTask) + self.syncBackgroundUpdateTask = UIBackgroundTaskIdentifier.invalid + } + + self.syncBackgroundUpdateTask = UIApplication.shared.beginBackgroundTask { + completeProcessing() + os_log("Accounts sync processing terminated for running too long.", log: self.log, type: .info) + } + + DispatchQueue.main.async { + AccountManager.shared.syncArticleStatusAll() { + completeProcessing() + } + } + } + +} + // MARK: Background Tasks private extension AppDelegate {