From 6b0bdf8ca0d8566678958f42f85fd4bac2b7d257 Mon Sep 17 00:00:00 2001 From: Maurice Parker Date: Mon, 18 Jan 2021 17:48:07 -0600 Subject: [PATCH] Only send statuses on quit and have a 2 second timeout. Fixes #2726 --- Account/Sources/Account/Account.swift | 11 +++++++++++ Account/Sources/Account/AccountManager.swift | 15 +++++++++++++++ Mac/AppDelegate.swift | 5 +++-- 3 files changed, 29 insertions(+), 2 deletions(-) diff --git a/Account/Sources/Account/Account.swift b/Account/Sources/Account/Account.swift index ec82c4225..5216e931c 100644 --- a/Account/Sources/Account/Account.swift +++ b/Account/Sources/Account/Account.swift @@ -431,6 +431,17 @@ public final class Account: DisplayNameProvider, UnreadCountProvider, Container, delegate.refreshAll(for: self, completion: completion) } + public func sendArticleStatus(completion: ((Result) -> Void)? = nil) { + delegate.sendArticleStatus(for: self) { result in + switch result { + case .success: + completion?(.success(())) + case .failure(let error): + completion?(.failure(error)) + } + } + } + public func syncArticleStatus(completion: ((Result) -> Void)? = nil) { delegate.sendArticleStatus(for: self) { [unowned self] result in switch result { diff --git a/Account/Sources/Account/AccountManager.swift b/Account/Sources/Account/AccountManager.swift index 39f3b6aef..bc1911947 100644 --- a/Account/Sources/Account/AccountManager.swift +++ b/Account/Sources/Account/AccountManager.swift @@ -293,6 +293,21 @@ public final class AccountManager: UnreadCountProvider { } + public func sendArticleStatusAll(completion: (() -> Void)? = nil) { + let group = DispatchGroup() + + activeAccounts.forEach { + group.enter() + $0.sendArticleStatus() { _ in + group.leave() + } + } + + group.notify(queue: DispatchQueue.global(qos: .background)) { + completion?() + } + } + public func syncArticleStatusAll(completion: (() -> Void)? = nil) { let group = DispatchGroup() diff --git a/Mac/AppDelegate.swift b/Mac/AppDelegate.swift index 88fcb3d0e..23b5e819c 100644 --- a/Mac/AppDelegate.swift +++ b/Mac/AppDelegate.swift @@ -313,11 +313,12 @@ class AppDelegate: NSObject, NSApplicationDelegate, NSUserInterfaceValidations, shuttingDown = true saveState() - AccountManager.shared.syncArticleStatusAll() { + AccountManager.shared.sendArticleStatusAll() { self.isShutDownSyncDone = true } - while !isShutDownSyncDone && RunLoop.current.run(mode: .default, before: .distantFuture) { } + let timeout = Date().addingTimeInterval(2) + while !isShutDownSyncDone && RunLoop.current.run(mode: .default, before: .distantFuture) && timeout > Date() { } } // MARK: Notifications