From 84412dfddd8ab1279150373a2cdf61beae77db4c Mon Sep 17 00:00:00 2001 From: Brent Simmons Date: Sat, 30 Sep 2023 22:13:15 -0700 Subject: [PATCH] Continue converting ArticlesDatabase to async/await. --- Account/Sources/Account/Account.swift | 48 +++++++++++-------- .../ArticlesDatabase/ArticlesDatabase.swift | 8 ++-- .../ArticlesDatabase/ArticlesTable.swift | 8 ++-- 3 files changed, 37 insertions(+), 27 deletions(-) diff --git a/Account/Sources/Account/Account.swift b/Account/Sources/Account/Account.swift index 0f2971a98..c3cc96f07 100644 --- a/Account/Sources/Account/Account.swift +++ b/Account/Sources/Account/Account.swift @@ -1152,7 +1152,16 @@ private extension Account { } func fetchTodayArticlesAsync(limit: Int?, _ completion: @escaping ArticleSetResultBlock) { - database.fetchTodayArticlesAsync(flattenedFeeds().feedIDs(), limit, completion) + Task { @MainActor in + let feedIDs = flattenedFeeds().feedIDs() + + do { + let articles = try await database.todayArticlesForFeeds(feedIDs, limit) + completion(.success(articles)) + } catch { + completion(.failure(error as! DatabaseError)) + } + } } @MainActor func fetchArticles(folder: Folder) throws -> Set
{ @@ -1225,7 +1234,7 @@ private extension Account { Task { @MainActor in completion(.failure(error as! DatabaseError)) } - } + } } } @@ -1277,25 +1286,26 @@ private extension Account { } func fetchUnreadArticlesAsync(forContainer container: Container, limit: Int?, _ completion: @escaping ArticleSetResultBlock) { - let feeds = container.flattenedFeeds() - database.fetchUnreadArticlesAsync(feeds.feedIDs(), limit) { [weak self] (articleSetResult) in - Task { @MainActor [weak self] in - switch articleSetResult { - case .success(let articles): + Task { @MainActor in + + let feeds = container.flattenedFeeds() + let feedIDs = feeds.feedIDs() - // We don't validate limit queries because they, by definition, won't correctly match the - // complete unread state for the given container. - if limit == nil { - self?.validateUnreadCountsAfterFetchingUnreadArticles(feeds, articles) - } + do { + let articles = try await database.unreadArticlesForFeeds(feedIDs, limit) - completion(.success(articles)) - case .failure(let databaseError): - completion(.failure(databaseError)) - } - } - } - } + // We don't validate limit queries because they, by definition, won't correctly match the + // complete unread state for the given container. + if limit == nil { + self.validateUnreadCountsAfterFetchingUnreadArticles(feeds, articles) + } + + completion(.success(articles)) + } catch { + completion(.failure(error as! DatabaseError)) + } + } + } @MainActor func validateUnreadCountsAfterFetchingUnreadArticles(_ feeds: Set, _ articles: Set
) { // Validate unread counts. This was the site of a performance slowdown: diff --git a/ArticlesDatabase/Sources/ArticlesDatabase/ArticlesDatabase.swift b/ArticlesDatabase/Sources/ArticlesDatabase/ArticlesDatabase.swift index 2ddafb434..55ab8751f 100644 --- a/ArticlesDatabase/Sources/ArticlesDatabase/ArticlesDatabase.swift +++ b/ArticlesDatabase/Sources/ArticlesDatabase/ArticlesDatabase.swift @@ -148,12 +148,12 @@ public typealias ArticleStatusesResultBlock = (ArticleStatusesResult) -> Void try await articlesTable.articlesForArticleIDs(articleIDs) } - public func fetchUnreadArticlesAsync(_ feedIDs: Set, _ limit: Int?, _ completion: @escaping ArticleSetResultBlock) { - articlesTable.fetchUnreadArticlesAsync(feedIDs, limit, completion) + public func unreadArticlesForFeeds(_ feedIDs: Set, _ limit: Int?) async throws -> Set
{ + try await articlesTable.unreadArticlesForFeeds(feedIDs, limit) } - public func fetchTodayArticlesAsync(_ feedIDs: Set, _ limit: Int?, _ completion: @escaping ArticleSetResultBlock) { - articlesTable.fetchArticlesSinceAsync(feedIDs, todayCutoffDate(), limit, completion) + public func todayArticlesForFeeds(_ feedIDs: Set, _ limit: Int?) async throws -> Set
{ + try await articlesTable.articlesForFeedIDsSince(feedIDs, todayCutoffDate(), limit) } public func fetchedStarredArticlesAsync(_ feedIDs: Set, _ limit: Int?, _ completion: @escaping ArticleSetResultBlock) { diff --git a/ArticlesDatabase/Sources/ArticlesDatabase/ArticlesTable.swift b/ArticlesDatabase/Sources/ArticlesDatabase/ArticlesTable.swift index 2bb9eeddb..0faf163f6 100644 --- a/ArticlesDatabase/Sources/ArticlesDatabase/ArticlesTable.swift +++ b/ArticlesDatabase/Sources/ArticlesDatabase/ArticlesTable.swift @@ -88,8 +88,8 @@ final class ArticlesTable: DatabaseTable { return try fetchArticles{ self.fetchUnreadArticlesBetween(feedIDs, limit, $0, before, after) } } - func fetchUnreadArticlesAsync(_ feedIDs: Set, _ limit: Int?, _ completion: @escaping ArticleSetResultBlock) { - fetchArticlesAsync({ self.fetchUnreadArticles(feedIDs, limit, $0) }, completion) + func unreadArticlesForFeeds(_ feedIDs: Set, _ limit: Int?) async throws -> Set
{ + try await articlesWithFetchMethod { self.fetchUnreadArticles(feedIDs, limit, $0) } } // MARK: - Fetching Today Articles @@ -98,8 +98,8 @@ final class ArticlesTable: DatabaseTable { return try fetchArticles{ self.fetchArticlesSince(feedIDs, cutoffDate, limit, $0) } } - func fetchArticlesSinceAsync(_ feedIDs: Set, _ cutoffDate: Date, _ limit: Int?, _ completion: @escaping ArticleSetResultBlock) { - fetchArticlesAsync({ self.fetchArticlesSince(feedIDs, cutoffDate, limit, $0) }, completion) + func articlesForFeedIDsSince(_ feedIDs: Set, _ cutoffDate: Date, _ limit: Int?) async throws -> Set
{ + try await articlesWithFetchMethod { self.fetchArticlesSince(feedIDs, cutoffDate, limit, $0) } } // MARK: - Fetching Starred Articles