From 46db34dfca749b7b06104318c589928a6d807ebf Mon Sep 17 00:00:00 2001 From: Brent Simmons Date: Sat, 30 Sep 2023 21:12:07 -0700 Subject: [PATCH] Continue converting ArticlesDatabase to async/await. --- Account/Sources/Account/Account.swift | 22 +++++++++---------- .../ArticlesDatabase/ArticlesDatabase.swift | 4 ++-- .../ArticlesDatabase/ArticlesTable.swift | 19 +++++++++++++--- 3 files changed, 29 insertions(+), 16 deletions(-) diff --git a/Account/Sources/Account/Account.swift b/Account/Sources/Account/Account.swift index 4837b3478..bf4112e13 100644 --- a/Account/Sources/Account/Account.swift +++ b/Account/Sources/Account/Account.swift @@ -1231,18 +1231,18 @@ private extension Account { return articles } - func fetchArticlesAsync(forContainer container: Container, _ completion: @escaping ArticleSetResultBlock) { + @MainActor func fetchArticlesAsync(forContainer container: Container, _ completion: @escaping ArticleSetResultBlock) { + let feeds = container.flattenedFeeds() - database.fetchArticlesAsync(feeds.feedIDs()) { [weak self] (articleSetResult) in - Task { @MainActor [weak self] in - switch articleSetResult { - case .success(let articles): - self?.validateUnreadCountsAfterFetchingUnreadArticles(feeds, articles) - completion(.success(articles)) - case .failure(let databaseError): - completion(.failure(databaseError)) - } - } + + Task { @MainActor in + do { + let articles = try await self.database.articlesForFeeds(feeds.feedIDs()) + self.validateUnreadCountsAfterFetchingUnreadArticles(feeds, articles) + completion(.success(articles)) + } catch { + completion(.failure(error as! DatabaseError)) + } } } diff --git a/ArticlesDatabase/Sources/ArticlesDatabase/ArticlesDatabase.swift b/ArticlesDatabase/Sources/ArticlesDatabase/ArticlesDatabase.swift index fc190675d..f1ae37711 100644 --- a/ArticlesDatabase/Sources/ArticlesDatabase/ArticlesDatabase.swift +++ b/ArticlesDatabase/Sources/ArticlesDatabase/ArticlesDatabase.swift @@ -140,8 +140,8 @@ public typealias ArticleStatusesResultBlock = (ArticleStatusesResult) -> Void try await articlesTable.articlesForFeed(feedID) } - public func fetchArticlesAsync(_ feedIDs: Set, _ completion: @escaping ArticleSetResultBlock) { - articlesTable.fetchArticlesAsync(feedIDs, completion) + public func articlesForFeeds(_ feedIDs: Set) async throws -> Set
{ + try await articlesTable.articlesForFeeds(feedIDs) } public func fetchArticlesAsync(articleIDs: Set, _ completion: @escaping ArticleSetResultBlock) { diff --git a/ArticlesDatabase/Sources/ArticlesDatabase/ArticlesTable.swift b/ArticlesDatabase/Sources/ArticlesDatabase/ArticlesTable.swift index d977639e0..979d2bc9c 100644 --- a/ArticlesDatabase/Sources/ArticlesDatabase/ArticlesTable.swift +++ b/ArticlesDatabase/Sources/ArticlesDatabase/ArticlesTable.swift @@ -61,6 +61,19 @@ final class ArticlesTable: DatabaseTable { } } + func articlesForFeeds(_ feedIDs: Set) async throws -> Set
{ + return try await withCheckedThrowingContinuation { continuation in + fetchArticlesAsync({ self.fetchArticlesForFeedIDs(feedIDs, $0) }) { articleSetResult in + switch articleSetResult { + case .success(let articles): + continuation.resume(returning: articles) + case .failure(let error): + continuation.resume(throwing: error) + } + } + } + } + func fetchArticles(_ feedID: String) throws -> Set
{ return try fetchArticles{ self.fetchArticlesForFeedID(feedID, $0) } } @@ -70,11 +83,11 @@ final class ArticlesTable: DatabaseTable { } func fetchArticles(_ feedIDs: Set) throws -> Set
{ - return try fetchArticles{ self.fetchArticles(feedIDs, $0) } + return try fetchArticles{ self.fetchArticlesForFeedIDs(feedIDs, $0) } } func fetchArticlesAsync(_ feedIDs: Set, _ completion: @escaping ArticleSetResultBlock) { - fetchArticlesAsync({ self.fetchArticles(feedIDs, $0) }, completion) + fetchArticlesAsync({ self.fetchArticlesForFeedIDs(feedIDs, $0) }, completion) } // MARK: - Fetching Articles by articleID @@ -875,7 +888,7 @@ private extension ArticlesTable { return articlesWithResultSet(resultSet, database) } - func fetchArticles(_ feedIDs: Set, _ database: FMDatabase) -> Set
{ + func fetchArticlesForFeedIDs(_ feedIDs: Set, _ database: FMDatabase) -> Set
{ // select * from articles natural join statuses where feedID in ('http://ranchero.com/xml/rss.xml') and read=0 if feedIDs.isEmpty { return Set
()