From 908652df51e9a9e83a956411eafa42be855d514e Mon Sep 17 00:00:00 2001 From: Brent Simmons Date: Mon, 16 Dec 2019 14:43:11 -0800 Subject: [PATCH] =?UTF-8?q?Make=20just=20one=20database=20call=20in=20fetc?= =?UTF-8?q?hUnreadCounts=20=E2=80=94=C2=A0use=20fetchAllUnreadCounts=20and?= =?UTF-8?q?=20process=20the=20UnreadCountDictionary.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../ArticlesDatabase/ArticlesTable.swift | 23 ++++++++----------- 1 file changed, 9 insertions(+), 14 deletions(-) diff --git a/Frameworks/ArticlesDatabase/ArticlesTable.swift b/Frameworks/ArticlesDatabase/ArticlesTable.swift index 43d1b5cd1..5ffea6889 100644 --- a/Frameworks/ArticlesDatabase/ArticlesTable.swift +++ b/Frameworks/ArticlesDatabase/ArticlesTable.swift @@ -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)) - } } } }