From 455f60a224f6d85c52ada412f51c52192a290ec0 Mon Sep 17 00:00:00 2001 From: Brent Simmons Date: Mon, 25 Mar 2024 23:11:39 -0700 Subject: [PATCH] Convert sendArticleStatusAll to async/await. --- Account/Sources/Account/Account.swift | 14 ++++++++++++++ Account/Sources/Account/AccountManager.swift | 19 ++++++++----------- Mac/AppDelegate.swift | 3 ++- 3 files changed, 24 insertions(+), 12 deletions(-) diff --git a/Account/Sources/Account/Account.swift b/Account/Sources/Account/Account.swift index 1c6f12b21..35a29e794 100644 --- a/Account/Sources/Account/Account.swift +++ b/Account/Sources/Account/Account.swift @@ -436,6 +436,20 @@ public enum FetchType { } } + public func sendArticleStatus() async throws { + + try await withCheckedThrowingContinuation { continuation in + self.sendArticleStatus { result in + switch result { + case .success: + continuation.resume() + case .failure(let error): + continuation.resume(throwing: error) + } + } + } + } + public func syncArticleStatus(completion: ((Result) -> Void)? = nil) { delegate.syncArticleStatus(for: self, completion: completion) } diff --git a/Account/Sources/Account/AccountManager.swift b/Account/Sources/Account/AccountManager.swift index 94b50045b..9f7465562 100644 --- a/Account/Sources/Account/AccountManager.swift +++ b/Account/Sources/Account/AccountManager.swift @@ -274,18 +274,15 @@ import Secrets } - public func sendArticleStatusAll(completion: (() -> Void)? = nil) { - let group = DispatchGroup() - - activeAccounts.forEach { - group.enter() - $0.sendArticleStatus() { _ in - group.leave() - } - } + public func sendArticleStatusAll() async { - group.notify(queue: DispatchQueue.global(qos: .background)) { - completion?() + await withTaskGroup(of: Void.self) { taskGroup in + + for account in activeAccounts { + taskGroup.addTask { + try? await account.sendArticleStatus() + } + } } } diff --git a/Mac/AppDelegate.swift b/Mac/AppDelegate.swift index 6651973e1..0dc48e60c 100644 --- a/Mac/AppDelegate.swift +++ b/Mac/AppDelegate.swift @@ -343,7 +343,8 @@ import Sparkle ArticleThemeDownloader.cleanUp() - accountManager.sendArticleStatusAll() { + Task { @MainActor in + await accountManager.sendArticleStatusAll() self.isShutDownSyncDone = true }