diff --git a/Account/Sources/Account/Account.swift b/Account/Sources/Account/Account.swift index bf4112e13..0f2971a98 100644 --- a/Account/Sources/Account/Account.swift +++ b/Account/Sources/Account/Account.swift @@ -1215,7 +1215,18 @@ private extension Account { } func fetchArticlesAsync(articleIDs: Set, _ completion: @escaping ArticleSetResultBlock) { - return database.fetchArticlesAsync(articleIDs: articleIDs, completion) + Task { @MainActor in + do { + let articles = try await database.articlesForArticleIDs(articleIDs) + Task { @MainActor in + completion(.success(articles)) + } + } catch { + Task { @MainActor in + completion(.failure(error as! DatabaseError)) + } + } + } } @MainActor func fetchUnreadArticles(feed: Feed) throws -> Set
{ diff --git a/ArticlesDatabase/Sources/ArticlesDatabase/ArticlesDatabase.swift b/ArticlesDatabase/Sources/ArticlesDatabase/ArticlesDatabase.swift index f1ae37711..2ddafb434 100644 --- a/ArticlesDatabase/Sources/ArticlesDatabase/ArticlesDatabase.swift +++ b/ArticlesDatabase/Sources/ArticlesDatabase/ArticlesDatabase.swift @@ -144,8 +144,8 @@ public typealias ArticleStatusesResultBlock = (ArticleStatusesResult) -> Void try await articlesTable.articlesForFeeds(feedIDs) } - public func fetchArticlesAsync(articleIDs: Set, _ completion: @escaping ArticleSetResultBlock) { - articlesTable.fetchArticlesAsync(articleIDs: articleIDs, completion) + public func articlesForArticleIDs(_ articleIDs: Set) async throws -> Set
{ + try await articlesTable.articlesForArticleIDs(articleIDs) } public func fetchUnreadArticlesAsync(_ feedIDs: Set, _ limit: Int?, _ completion: @escaping ArticleSetResultBlock) { diff --git a/ArticlesDatabase/Sources/ArticlesDatabase/ArticlesTable.swift b/ArticlesDatabase/Sources/ArticlesDatabase/ArticlesTable.swift index 979d2bc9c..2bb9eeddb 100644 --- a/ArticlesDatabase/Sources/ArticlesDatabase/ArticlesTable.swift +++ b/ArticlesDatabase/Sources/ArticlesDatabase/ArticlesTable.swift @@ -49,59 +49,33 @@ final class ArticlesTable: DatabaseTable { // MARK: - Fetching Articles for Feed func articlesForFeed(_ feedID: String) async throws -> Set
{ - return try await withCheckedThrowingContinuation { continuation in - fetchArticlesAsync({ self.fetchArticlesForFeedID(feedID, $0) }) { articleSetResult in - switch articleSetResult { - case .success(let articles): - continuation.resume(returning: articles) - case .failure(let error): - continuation.resume(throwing: error) - } - } - } + try await articlesWithFetchMethod { self.fetchArticlesForFeedID(feedID, $0) } } 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) - } - } - } + try await articlesWithFetchMethod { self.fetchArticlesForFeedIDs(feedIDs, $0) } } func fetchArticles(_ feedID: String) throws -> Set
{ return try fetchArticles{ self.fetchArticlesForFeedID(feedID, $0) } } - func fetchArticlesAsync(_ feedID: String, _ completion: @escaping ArticleSetResultBlock) { - fetchArticlesAsync({ self.fetchArticlesForFeedID(feedID, $0) }, completion) - } - func fetchArticles(_ feedIDs: Set) throws -> Set
{ return try fetchArticles{ self.fetchArticlesForFeedIDs(feedIDs, $0) } } - func fetchArticlesAsync(_ feedIDs: Set, _ completion: @escaping ArticleSetResultBlock) { - fetchArticlesAsync({ self.fetchArticlesForFeedIDs(feedIDs, $0) }, completion) - } - // MARK: - Fetching Articles by articleID func fetchArticles(articleIDs: Set) throws -> Set
{ - return try fetchArticles{ self.fetchArticles(articleIDs: articleIDs, $0) } + try fetchArticles{ self.fetchArticlesForArticleIDs(articleIDs, $0) } } func fetchArticlesBetween(articleIDs: Set, before: Date?, after: Date?) throws -> Set
{ - return try fetchArticles{ self.fetchArticlesBetween(articleIDs: articleIDs, before: before, after: after, $0) } + try fetchArticles{ self.fetchArticlesBetween(articleIDs: articleIDs, before: before, after: after, $0) } } - func fetchArticlesAsync(articleIDs: Set, _ completion: @escaping ArticleSetResultBlock) { - return fetchArticlesAsync({ self.fetchArticles(articleIDs: articleIDs, $0) }, completion) + func articlesForArticleIDs(_ articleIDs: Set) async throws -> Set
{ + try await articlesWithFetchMethod { self.fetchArticlesForArticleIDs(articleIDs, $0) } } // MARK: - Fetching Unread Articles @@ -349,7 +323,7 @@ final class ArticlesTable: DatabaseTable { } let incomingArticleIDs = incomingArticles.articleIDs() - let fetchedArticles = self.fetchArticles(articleIDs: incomingArticleIDs, database) //4 + let fetchedArticles = self.fetchArticlesForArticleIDs(incomingArticleIDs, database) //4 let fetchedArticlesDictionary = fetchedArticles.dictionary() let newArticles = self.findAndSaveNewArticles(incomingArticles, fetchedArticlesDictionary, database) //5 @@ -761,6 +735,22 @@ private extension ArticlesTable { return articlesCount } + private func articlesWithFetchMethod(_ fetchMethod: @escaping ArticlesFetchMethod) async throws -> Set
{ + try await withCheckedThrowingContinuation { continuation in + queue.runInDatabase { databaseResult in + + switch databaseResult { + case .success(let database): + let articles = fetchMethod(database) + continuation.resume(returning: articles) + case .failure(let databaseError): + continuation.resume(throwing: databaseError) + } + } + } + } + + private func fetchArticlesAsync(_ fetchMethod: @escaping ArticlesFetchMethod, _ completion: @escaping ArticleSetResultBlock) { queue.runInDatabase { databaseResult in @@ -941,7 +931,7 @@ private extension ArticlesTable { return fetchArticlesWithWhereClause(database, whereClause: "articles.feedID = ?", parameters: [feedID as AnyObject]) } - func fetchArticles(articleIDs: Set, _ database: FMDatabase) -> Set
{ + func fetchArticlesForArticleIDs(_ articleIDs: Set, _ database: FMDatabase) -> Set
{ if articleIDs.isEmpty { return Set
() }