Fix widget data encoding crash — and possibly other bugs — by making sure that `fetchArticlesAsync` calls its callback just once. (The widget data encoder was crashing with multiple calls in the failure case, since it ended up having unbalanced DispatchGroup enter and leave calls.)

This commit is contained in:
Brent Simmons 2023-01-21 14:49:08 -08:00
parent 1cfc4532d7
commit 6c781f3a63
1 changed files with 18 additions and 7 deletions

View File

@ -364,31 +364,42 @@ public final class AccountManager: UnreadCountProvider {
return articles
}
public func fetchArticlesAsync(_ fetchType: FetchType, _ completion: @escaping ArticleSetResultBlock) {
public func fetchArticlesAsync(_ fetchType: FetchType, _ completion: @escaping ArticleSetResultBlock) {
precondition(Thread.isMainThread)
var allFetchedArticles = Set<Article>()
let numberOfAccounts = activeAccounts.count
var accountsReporting = 0
var didCallCompletionBlock = false
var databaseFetchDidFail = false
func callCompletion(_ result: ArticleSetResult) {
guard !didCallCompletionBlock else {
return
}
completion(result)
didCallCompletionBlock = true
}
guard numberOfAccounts > 0 else {
completion(.success(allFetchedArticles))
callCompletion(.success(allFetchedArticles))
return
}
for account in activeAccounts {
account.fetchArticlesAsync(fetchType) { (articleSetResult) in
precondition(Thread.isMainThread)
accountsReporting += 1
switch articleSetResult {
case .success(let articles):
allFetchedArticles.formUnion(articles)
if accountsReporting == numberOfAccounts {
completion(.success(allFetchedArticles))
if accountsReporting == numberOfAccounts && !databaseFetchDidFail {
callCompletion(.success(allFetchedArticles))
}
case .failure(let databaseError):
completion(.failure(databaseError))
return
databaseFetchDidFail = true
callCompletion(.failure(databaseError))
}
}
}