Make just one database call in fetchUnreadCounts — use fetchAllUnreadCounts and process the UnreadCountDictionary.

This commit is contained in:
Brent Simmons 2019-12-16 14:43:11 -08:00
parent aa5859ff27
commit 908652df51
1 changed files with 9 additions and 14 deletions

View File

@ -295,26 +295,21 @@ final class ArticlesTable: DatabaseTable {
return
}
queue.runInDatabase { databaseResult in
fetchAllUnreadCounts { (unreadCountsResult) in
func makeDatabaseCalls(_ database: FMDatabase) {
var unreadCountDictionary = UnreadCountDictionary()
func createUnreadCountDictionary(_ unreadCountDictionary: UnreadCountDictionary) -> UnreadCountDictionary {
var d = UnreadCountDictionary()
for webFeedID in webFeedIDs {
unreadCountDictionary[webFeedID] = self.fetchUnreadCount(webFeedID, database)
}
DispatchQueue.main.async {
completion(.success(unreadCountDictionary))
d[webFeedID] = unreadCountDictionary[webFeedID] ?? 0
}
return d
}
switch databaseResult {
case .success(let database):
makeDatabaseCalls(database)
case .failure(let databaseError):
DispatchQueue.main.async {
switch unreadCountsResult {
case .success(let unreadCountDictionary):
completion(.success(createUnreadCountDictionary(unreadCountDictionary)))
case .failure(let databaseError):
completion(.failure(databaseError))
}
}
}
}