Ensure that database status inserts are complete before continuing processing. Fixes #2659

This commit is contained in:
Maurice Parker 2020-12-10 17:28:19 -06:00
parent 9790b7840d
commit ea407fd8da

View File

@ -189,8 +189,9 @@ final class FeedbinAccountDelegate: AccountDelegate {
caller.retrieveUnreadEntries() { result in caller.retrieveUnreadEntries() { result in
switch result { switch result {
case .success(let articleIDs): case .success(let articleIDs):
self.syncArticleReadState(account: account, articleIDs: articleIDs) self.syncArticleReadState(account: account, articleIDs: articleIDs) {
group.leave() group.leave()
}
case .failure(let error): case .failure(let error):
errorOccurred = true errorOccurred = true
os_log(.info, log: self.log, "Retrieving unread entries failed: %@.", error.localizedDescription) os_log(.info, log: self.log, "Retrieving unread entries failed: %@.", error.localizedDescription)
@ -203,8 +204,9 @@ final class FeedbinAccountDelegate: AccountDelegate {
caller.retrieveStarredEntries() { result in caller.retrieveStarredEntries() { result in
switch result { switch result {
case .success(let articleIDs): case .success(let articleIDs):
self.syncArticleStarredState(account: account, articleIDs: articleIDs) self.syncArticleStarredState(account: account, articleIDs: articleIDs) {
group.leave() group.leave()
}
case .failure(let error): case .failure(let error):
errorOccurred = true errorOccurred = true
os_log(.info, log: self.log, "Retrieving starred entries failed: %@.", error.localizedDescription) os_log(.info, log: self.log, "Retrieving starred entries failed: %@.", error.localizedDescription)
@ -1245,8 +1247,9 @@ private extension FeedbinAccountDelegate {
} }
func syncArticleReadState(account: Account, articleIDs: [Int]?) { func syncArticleReadState(account: Account, articleIDs: [Int]?, completion: @escaping (() -> Void)) {
guard let articleIDs = articleIDs else { guard let articleIDs = articleIDs else {
completion()
return return
} }
@ -1262,13 +1265,26 @@ private extension FeedbinAccountDelegate {
return return
} }
let group = DispatchGroup()
// Mark articles as unread // Mark articles as unread
let deltaUnreadArticleIDs = updatableFeedbinUnreadArticleIDs.subtracting(currentUnreadArticleIDs) let deltaUnreadArticleIDs = updatableFeedbinUnreadArticleIDs.subtracting(currentUnreadArticleIDs)
account.markAsUnread(deltaUnreadArticleIDs) group.enter()
account.markAsUnread(deltaUnreadArticleIDs) { _ in
group.leave()
}
// Mark articles as read // Mark articles as read
let deltaReadArticleIDs = currentUnreadArticleIDs.subtracting(updatableFeedbinUnreadArticleIDs) let deltaReadArticleIDs = currentUnreadArticleIDs.subtracting(updatableFeedbinUnreadArticleIDs)
account.markAsRead(deltaReadArticleIDs) group.enter()
account.markAsRead(deltaReadArticleIDs) { _ in
group.leave()
}
group.notify(queue: DispatchQueue.main) {
completion()
}
} }
} }
@ -1284,8 +1300,9 @@ private extension FeedbinAccountDelegate {
} }
func syncArticleStarredState(account: Account, articleIDs: [Int]?) { func syncArticleStarredState(account: Account, articleIDs: [Int]?, completion: @escaping (() -> Void)) {
guard let articleIDs = articleIDs else { guard let articleIDs = articleIDs else {
completion()
return return
} }
@ -1294,20 +1311,32 @@ private extension FeedbinAccountDelegate {
func process(_ pendingArticleIDs: Set<String>) { func process(_ pendingArticleIDs: Set<String>) {
let feedbinStarredArticleIDs = Set(articleIDs.map { String($0) } ) let feedbinStarredArticleIDs = Set(articleIDs.map { String($0) } )
let updatableFeedbinUnreadArticleIDs = feedbinStarredArticleIDs.subtracting(pendingArticleIDs) let updatableFeedbinStarredArticleIDs = feedbinStarredArticleIDs.subtracting(pendingArticleIDs)
account.fetchStarredArticleIDs { articleIDsResult in account.fetchStarredArticleIDs { articleIDsResult in
guard let currentStarredArticleIDs = try? articleIDsResult.get() else { guard let currentStarredArticleIDs = try? articleIDsResult.get() else {
return return
} }
let group = DispatchGroup()
// Mark articles as starred // Mark articles as starred
let deltaStarredArticleIDs = updatableFeedbinUnreadArticleIDs.subtracting(currentStarredArticleIDs) let deltaStarredArticleIDs = updatableFeedbinStarredArticleIDs.subtracting(currentStarredArticleIDs)
account.markAsStarred(deltaStarredArticleIDs) group.enter()
account.markAsStarred(deltaStarredArticleIDs) { _ in
group.leave()
}
// Mark articles as unstarred // Mark articles as unstarred
let deltaUnstarredArticleIDs = currentStarredArticleIDs.subtracting(updatableFeedbinUnreadArticleIDs) let deltaUnstarredArticleIDs = currentStarredArticleIDs.subtracting(updatableFeedbinStarredArticleIDs)
account.markAsUnstarred(deltaUnstarredArticleIDs) group.enter()
account.markAsUnstarred(deltaUnstarredArticleIDs) { _ in
group.leave()
}
group.notify(queue: DispatchQueue.main) {
completion()
}
} }
} }