From 264db509819c027d4dcd4bf0a98a1d98522beb7a Mon Sep 17 00:00:00 2001 From: Brent Simmons Date: Wed, 4 Oct 2023 21:44:58 -0700 Subject: [PATCH] Continue converting ArticlesDatabase to async/await. --- Account/Sources/Account/Account.swift | 15 ++-- .../ArticlesDatabase/ArticlesDatabase.swift | 2 +- .../ArticlesDatabase/ArticlesTable.swift | 71 +------------------ 3 files changed, 12 insertions(+), 76 deletions(-) diff --git a/Account/Sources/Account/Account.swift b/Account/Sources/Account/Account.swift index ec9392bed..576de244e 100644 --- a/Account/Sources/Account/Account.swift +++ b/Account/Sources/Account/Account.swift @@ -896,16 +896,17 @@ public enum FetchType { completion?(nil) return } - database.createStatusesIfNeeded(articleIDs: articleIDs) { error in - if let error = error { - completion?(error) - return + + Task { @MainActor in + do { + let statusesDictionary = try await database.createStatusesIfNeeded(articleIDs: articleIDs) + completion?(nil) + } catch { + completion?(error as? DatabaseError) } - self.noteStatusesForArticleIDsDidChange(articleIDs) - completion?(nil) } } - + /// Mark articleIDs statuses based on statusKey and flag. /// Will create statuses in the database and in memory as needed. Sends a .StatusesDidChange notification. func mark(articleIDs: Set, statusKey: ArticleStatus.Key, flag: Bool, completion: DatabaseCompletionBlock? = nil) { diff --git a/ArticlesDatabase/Sources/ArticlesDatabase/ArticlesDatabase.swift b/ArticlesDatabase/Sources/ArticlesDatabase/ArticlesDatabase.swift index 988129cf6..669ca255c 100644 --- a/ArticlesDatabase/Sources/ArticlesDatabase/ArticlesDatabase.swift +++ b/ArticlesDatabase/Sources/ArticlesDatabase/ArticlesDatabase.swift @@ -249,7 +249,7 @@ public typealias ArticleStatusesResultBlock = (ArticleStatusesResult) -> Void /// Create statuses for specified articleIDs. For existing statuses, don’t do anything. /// For newly-created statuses, mark them as read and not-starred. - public func createStatusesIfNeeded(articleIDs: Set) async throws { + public func createStatusesIfNeeded(articleIDs: Set) async throws -> [String: ArticleStatus] { try await articlesTable.createStatusesIfNeeded(articleIDs) } diff --git a/ArticlesDatabase/Sources/ArticlesDatabase/ArticlesTable.swift b/ArticlesDatabase/Sources/ArticlesDatabase/ArticlesTable.swift index 54a5d0d97..a821ddf47 100644 --- a/ArticlesDatabase/Sources/ArticlesDatabase/ArticlesTable.swift +++ b/ArticlesDatabase/Sources/ArticlesDatabase/ArticlesTable.swift @@ -493,37 +493,6 @@ final class ArticlesTable: DatabaseTable { } } - func fetchStarredAndUnreadCount(_ feedIDs: Set, _ completion: @escaping SingleUnreadCountCompletionBlock) { - if feedIDs.isEmpty { - completion(.success(0)) - return - } - - queue.runInDatabase { databaseResult in - - func makeDatabaseCalls(_ database: FMDatabase) { - let placeholders = NSString.rs_SQLValueList(withPlaceholders: UInt(feedIDs.count))! - let sql = "select count(*) from articles natural join statuses where feedID in \(placeholders) and read=0 and starred=1;" - let parameters = Array(feedIDs) as [Any] - - let unreadCount = self.numberWithSQLAndParameters(sql, parameters, in: database) - - DispatchQueue.main.async { - completion(.success(unreadCount)) - } - } - - switch databaseResult { - case .success(let database): - makeDatabaseCalls(database) - case .failure(let databaseError): - DispatchQueue.main.async { - completion(.failure(databaseError)) - } - } - } - } - // MARK: - Statuses func fetchUnreadArticleIDsAsync() async throws -> Set { @@ -574,30 +543,14 @@ final class ArticlesTable: DatabaseTable { } } - func mark(_ articleIDs: Set, _ statusKey: ArticleStatus.Key, _ flag: Bool, _ completion: DatabaseCompletionBlock?) { - queue.runInTransaction { databaseResult in - switch databaseResult { - case .success(let database): - self.statusesTable.mark(articleIDs, statusKey, flag, database) - DispatchQueue.main.async { - completion?(nil) - } - case .failure(let databaseError): - DispatchQueue.main.async { - completion?(databaseError) - } - } - } - } - - func createStatusesIfNeeded(_ articleIDs: Set) async throws { + func createStatusesIfNeeded(_ articleIDs: Set) async throws -> [String: ArticleStatus]{ try await withCheckedThrowingContinuation { continuation in queue.runInTransaction { databaseResult in switch databaseResult { case .success(let database): - let _ = self.statusesTable.ensureStatusesForArticleIDs(articleIDs, true, database) - continuation.resume(returning: nil) + let statusesDictionary = self.statusesTable.ensureStatusesForArticleIDs(articleIDs, true, database) + continuation.resume(returning: statusesDictionary) case .failure(let databaseError): continuation.resume(throwing: databaseError) } @@ -809,24 +762,6 @@ private extension ArticlesTable { } } - - private func fetchArticlesAsync(_ fetchMethod: @escaping ArticlesFetchMethod, _ completion: @escaping ArticleSetResultBlock) { - queue.runInDatabase { databaseResult in - - switch databaseResult { - case .success(let database): - let articles = fetchMethod(database) - DispatchQueue.main.async { - completion(.success(articles)) - } - case .failure(let databaseError): - DispatchQueue.main.async { - completion(.failure(databaseError)) - } - } - } - } - func articlesWithResultSet(_ resultSet: FMResultSet, _ database: FMDatabase) -> Set
{ var cachedArticles = Set
() var fetchedArticles = Set
()