From b0ae2255d1dfc4323b3114804353f7f312c68907 Mon Sep 17 00:00:00 2001 From: Maurice Parker Date: Thu, 10 Dec 2020 17:38:57 -0600 Subject: [PATCH] Ensure that database status inserts are complete before continuing processing. --- .../NewsBlurAccountDelegate+Internal.swift | 46 +++++++++++++++---- .../NewsBlur/NewsBlurAccountDelegate.swift | 10 ++-- 2 files changed, 44 insertions(+), 12 deletions(-) diff --git a/Account/Sources/Account/NewsBlur/Internals/NewsBlurAccountDelegate+Internal.swift b/Account/Sources/Account/NewsBlur/Internals/NewsBlurAccountDelegate+Internal.swift index c55e160e2..c86b30b71 100644 --- a/Account/Sources/Account/NewsBlur/Internals/NewsBlurAccountDelegate+Internal.swift +++ b/Account/Sources/Account/NewsBlur/Internals/NewsBlurAccountDelegate+Internal.swift @@ -311,8 +311,11 @@ extension NewsBlurAccountDelegate { } } - func syncStoryReadState(account: Account, hashes: [NewsBlurStoryHash]?) { - guard let hashes = hashes else { return } + func syncStoryReadState(account: Account, hashes: [NewsBlurStoryHash]?, completion: @escaping (() -> Void)) { + guard let hashes = hashes else { + completion() + return + } database.selectPendingReadStatusArticleIDs() { result in func process(_ pendingStoryHashes: Set) { @@ -325,13 +328,25 @@ extension NewsBlurAccountDelegate { return } + let group = DispatchGroup() + // Mark articles as unread let deltaUnreadArticleIDs = updatableNewsBlurUnreadStoryHashes.subtracting(currentUnreadArticleIDs) - account.markAsUnread(deltaUnreadArticleIDs) + group.enter() + account.markAsUnread(deltaUnreadArticleIDs) { _ in + group.leave() + } // Mark articles as read let deltaReadArticleIDs = currentUnreadArticleIDs.subtracting(updatableNewsBlurUnreadStoryHashes) - account.markAsRead(deltaReadArticleIDs) + group.enter() + account.markAsRead(deltaReadArticleIDs) { _ in + group.leave() + } + + group.notify(queue: DispatchQueue.main) { + completion() + } } } @@ -344,8 +359,11 @@ extension NewsBlurAccountDelegate { } } - func syncStoryStarredState(account: Account, hashes: [NewsBlurStoryHash]?) { - guard let hashes = hashes else { return } + func syncStoryStarredState(account: Account, hashes: [NewsBlurStoryHash]?, completion: @escaping (() -> Void)) { + guard let hashes = hashes else { + completion() + return + } database.selectPendingStarredStatusArticleIDs() { result in func process(_ pendingStoryHashes: Set) { @@ -358,13 +376,25 @@ extension NewsBlurAccountDelegate { return } + let group = DispatchGroup() + // Mark articles as starred let deltaStarredArticleIDs = updatableNewsBlurUnreadStoryHashes.subtracting(currentStarredArticleIDs) - account.markAsStarred(deltaStarredArticleIDs) + group.enter() + account.markAsStarred(deltaStarredArticleIDs) { _ in + group.leave() + } // Mark articles as unstarred let deltaUnstarredArticleIDs = currentStarredArticleIDs.subtracting(updatableNewsBlurUnreadStoryHashes) - account.markAsUnstarred(deltaUnstarredArticleIDs) + group.enter() + account.markAsUnstarred(deltaUnstarredArticleIDs) { _ in + group.leave() + } + + group.notify(queue: DispatchQueue.main) { + completion() + } } } diff --git a/Account/Sources/Account/NewsBlur/NewsBlurAccountDelegate.swift b/Account/Sources/Account/NewsBlur/NewsBlurAccountDelegate.swift index 638f3d19f..e0b5f2f6f 100644 --- a/Account/Sources/Account/NewsBlur/NewsBlurAccountDelegate.swift +++ b/Account/Sources/Account/NewsBlur/NewsBlurAccountDelegate.swift @@ -197,8 +197,9 @@ final class NewsBlurAccountDelegate: AccountDelegate { caller.retrieveUnreadStoryHashes { result in switch result { case .success(let storyHashes): - self.syncStoryReadState(account: account, hashes: storyHashes) - group.leave() + self.syncStoryReadState(account: account, hashes: storyHashes) { + group.leave() + } case .failure(let error): errorOccurred = true os_log(.info, log: self.log, "Retrieving unread stories failed: %@.", error.localizedDescription) @@ -210,8 +211,9 @@ final class NewsBlurAccountDelegate: AccountDelegate { caller.retrieveStarredStoryHashes { result in switch result { case .success(let storyHashes): - self.syncStoryStarredState(account: account, hashes: storyHashes) - group.leave() + self.syncStoryStarredState(account: account, hashes: storyHashes) { + group.leave() + } case .failure(let error): errorOccurred = true os_log(.info, log: self.log, "Retrieving starred stories failed: %@.", error.localizedDescription)