Only send statuses on quit and have a 2 second timeout. Fixes #2726

This commit is contained in:
Maurice Parker 2021-01-18 17:48:07 -06:00
parent de93658fc2
commit 6b0bdf8ca0
3 changed files with 29 additions and 2 deletions

View File

@ -431,6 +431,17 @@ public final class Account: DisplayNameProvider, UnreadCountProvider, Container,
delegate.refreshAll(for: self, completion: completion)
}
public func sendArticleStatus(completion: ((Result<Void, Error>) -> 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, Error>) -> Void)? = nil) {
delegate.sendArticleStatus(for: self) { [unowned self] result in
switch result {

View File

@ -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()

View File

@ -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