From d794eeafb78a9c3531b35d5d2dea9f99b987f22a Mon Sep 17 00:00:00 2001 From: Brent Simmons Date: Thu, 3 Sep 2026 11:49:15 -0700 Subject: [PATCH] Cancel the status sync on expiration and end its background task exactly once. --- iOS/AppDelegate.swift | 38 +++++++++++++++++++------------------- 1 file changed, 19 insertions(+), 19 deletions(-) diff --git a/iOS/AppDelegate.swift b/iOS/AppDelegate.swift index 79fa0a567..d886cf3cf 100644 --- a/iOS/AppDelegate.swift +++ b/iOS/AppDelegate.swift @@ -26,7 +26,6 @@ import Images private let backgroundTaskDispatchQueue = DispatchQueue.init(label: "BGTaskScheduler") private var waitBackgroundUpdateTask = UIBackgroundTaskIdentifier.invalid - private var syncBackgroundUpdateTask = UIBackgroundTaskIdentifier.invalid var shuttingDown = false { didSet { @@ -326,32 +325,33 @@ private extension AppDelegate { } func syncArticleStatus() { - guard !isSyncArticleStatusRunning else { return } + guard !isSyncArticleStatusRunning else { + return + } isSyncArticleStatusRunning = true - let completeProcessing = { [weak self] in - guard let self else { + var backgroundTaskIdentifier = UIBackgroundTaskIdentifier.invalid + + /// Make sure this run’s background task is ended exactly once. + func endSyncBackgroundTask() { + guard backgroundTaskIdentifier != .invalid else { return } - self.isSyncArticleStatusRunning = false - UIApplication.shared.endBackgroundTask(self.syncBackgroundUpdateTask) - self.syncBackgroundUpdateTask = UIBackgroundTaskIdentifier.invalid + UIApplication.shared.endBackgroundTask(backgroundTaskIdentifier) + backgroundTaskIdentifier = .invalid } - self.syncBackgroundUpdateTask = UIApplication.shared.beginBackgroundTask { [weak self] in - Task { @MainActor in - guard let self = self else { return } - self.isSyncArticleStatusRunning = false - UIApplication.shared.endBackgroundTask(self.syncBackgroundUpdateTask) - self.syncBackgroundUpdateTask = UIBackgroundTaskIdentifier.invalid - Self.logger.info("Accounts sync processing terminated for running too long.") - } - } - - Task { @MainActor in + let syncTask = Task { @MainActor in await AccountManager.shared.syncArticleStatusAll() - completeProcessing() + isSyncArticleStatusRunning = false + endSyncBackgroundTask() + } + + backgroundTaskIdentifier = UIApplication.shared.beginBackgroundTask(withName: "Sync Article Status") { + syncTask.cancel() + endSyncBackgroundTask() + Self.logger.info("Accounts sync processing terminated for running too long.") } }