diff --git a/Account/Sources/Account/Account.swift b/Account/Sources/Account/Account.swift index 5cd088bf3..1c6f12b21 100644 --- a/Account/Sources/Account/Account.swift +++ b/Account/Sources/Account/Account.swift @@ -440,6 +440,20 @@ public enum FetchType { delegate.syncArticleStatus(for: self, completion: completion) } + public func syncArticleStatus() async throws { + + try await withCheckedThrowingContinuation { continuation in + self.syncArticleStatus { result in + switch result { + case .success: + continuation.resume() + case .failure(let error): + continuation.resume(throwing: error) + } + } + } + } + public func importOPML(_ opmlFile: URL, completion: @escaping (Result) -> Void) { guard !delegate.isOPMLImportInProgress else { completion(.failure(AccountError.opmlImportInProgress)) diff --git a/Account/Sources/Account/AccountManager.swift b/Account/Sources/Account/AccountManager.swift index de7a1ae2f..94b50045b 100644 --- a/Account/Sources/Account/AccountManager.swift +++ b/Account/Sources/Account/AccountManager.swift @@ -289,21 +289,18 @@ import Secrets } } - public func syncArticleStatusAll(completion: (() -> Void)? = nil) { - let group = DispatchGroup() - - activeAccounts.forEach { - group.enter() - $0.syncArticleStatus() { _ in - group.leave() + public func syncArticleStatusAll() async { + + await withTaskGroup(of: Void.self) { taskGroup in + + for account in activeAccounts { + taskGroup.addTask { + try? await account.syncArticleStatus() + } } } - - group.notify(queue: DispatchQueue.global(qos: .background)) { - completion?() - } } - + public func saveAll() { accounts.forEach { $0.save() } } diff --git a/Shared/Timer/ArticleStatusSyncTimer.swift b/Shared/Timer/ArticleStatusSyncTimer.swift index b465fb552..062ae8ad9 100644 --- a/Shared/Timer/ArticleStatusSyncTimer.swift +++ b/Shared/Timer/ArticleStatusSyncTimer.swift @@ -68,8 +68,8 @@ import Account lastTimedRefresh = Date() update() - AccountManager.shared.syncArticleStatusAll() - + Task { @MainActor in + await AccountManager.shared.syncArticleStatusAll() + } } - } diff --git a/iOS/AppDelegate.swift b/iOS/AppDelegate.swift index f19d2809d..010505729 100644 --- a/iOS/AppDelegate.swift +++ b/iOS/AppDelegate.swift @@ -187,7 +187,9 @@ class AppDelegate: UIResponder, UIApplicationDelegate, UNUserNotificationCenterD if Date() > lastRefresh.addingTimeInterval(15 * 60) { accountManager.refreshAll(errorHandler: ErrorHandler.log) } else { - accountManager.syncArticleStatusAll() + Task { @MainActor in + await accountManager.syncArticleStatusAll() + } } } else { accountManager.refreshAll(errorHandler: ErrorHandler.log) @@ -331,9 +333,8 @@ private extension AppDelegate { } Task { @MainActor in - self.accountManager.syncArticleStatusAll() { - completeProcessing() - } + await self.accountManager.syncArticleStatusAll() + completeProcessing() } }