diff --git a/Account/Sources/Account/Account.swift b/Account/Sources/Account/Account.swift index 45427ae55..ec9392bed 100644 --- a/Account/Sources/Account/Account.swift +++ b/Account/Sources/Account/Account.swift @@ -873,15 +873,17 @@ public enum FetchType { return } - database.mark(articles, statusKey: statusKey, flag: flag) { result in - switch result { - case .success(let updatedStatuses): + Task { @MainActor in + do { + let updatedStatuses = try await self.database.mark(articles, statusKey: statusKey, flag: flag) let updatedArticleIDs = updatedStatuses.articleIDs() let updatedArticles = Set(articles.filter{ updatedArticleIDs.contains($0.articleID) }) - self.noteStatusesForArticlesDidChange(updatedArticles) + if !updatedArticles.isEmpty { + self.noteStatusesForArticlesDidChange(updatedArticles) + } completion(.success(updatedArticles)) - case .failure(let error): - completion(.failure(error)) + } catch { + completion(.failure(error as! DatabaseError)) } } } diff --git a/ArticlesDatabase/Sources/ArticlesDatabase/ArticlesDatabase.swift b/ArticlesDatabase/Sources/ArticlesDatabase/ArticlesDatabase.swift index 409f2088b..992fef5fa 100644 --- a/ArticlesDatabase/Sources/ArticlesDatabase/ArticlesDatabase.swift +++ b/ArticlesDatabase/Sources/ArticlesDatabase/ArticlesDatabase.swift @@ -239,8 +239,8 @@ public typealias ArticleStatusesResultBlock = (ArticleStatusesResult) -> Void try await articlesTable.fetchArticleIDsForStatusesWithoutArticlesNewerThanCutoffDate() } - public func mark(_ articles: Set
, statusKey: ArticleStatus.Key, flag: Bool, completion: @escaping ArticleStatusesResultBlock) { - return articlesTable.mark(articles, statusKey, flag, completion) + public func mark(_ articles: Set
, statusKey: ArticleStatus.Key, flag: Bool) async throws -> Set { + try await articlesTable.mark(articles, statusKey, flag) } public func markArticleIDs(_ articleIDs: Set, statusKey: ArticleStatus.Key, flag: Bool) async throws { diff --git a/ArticlesDatabase/Sources/ArticlesDatabase/ArticlesTable.swift b/ArticlesDatabase/Sources/ArticlesDatabase/ArticlesTable.swift index 829640831..a76ffb3e5 100644 --- a/ArticlesDatabase/Sources/ArticlesDatabase/ArticlesTable.swift +++ b/ArticlesDatabase/Sources/ArticlesDatabase/ArticlesTable.swift @@ -542,17 +542,16 @@ final class ArticlesTable: DatabaseTable { try await statusesTable.fetchArticleIDsForStatusesWithoutArticlesNewerThan(articleCutoffDate) } - func mark(_ articles: Set
, _ statusKey: ArticleStatus.Key, _ flag: Bool, _ completion: @escaping ArticleStatusesResultBlock) { - self.queue.runInTransaction { databaseResult in - switch databaseResult { - case .success(let database): - let statuses = self.statusesTable.mark(articles.statuses(), statusKey, flag, database) - DispatchQueue.main.async { - completion(.success(statuses ?? Set())) - } - case .failure(let databaseError): - DispatchQueue.main.async { - completion(.failure(databaseError)) + func mark(_ articles: Set
, _ statusKey: ArticleStatus.Key, _ flag: Bool) async throws -> Set { + + try await withCheckedThrowingContinuation { continuation in + self.queue.runInTransaction { databaseResult in + switch databaseResult { + case .success(let database): + let statuses = self.statusesTable.mark(articles.statuses(), statusKey, flag, database) + continuation.resume(returning: statuses) + case .failure(let databaseError): + continuation.resume(throwing: databaseError) } } } diff --git a/ArticlesDatabase/Sources/ArticlesDatabase/StatusesTable.swift b/ArticlesDatabase/Sources/ArticlesDatabase/StatusesTable.swift index 5a8454f46..743b55c77 100644 --- a/ArticlesDatabase/Sources/ArticlesDatabase/StatusesTable.swift +++ b/ArticlesDatabase/Sources/ArticlesDatabase/StatusesTable.swift @@ -62,7 +62,7 @@ final class StatusesTable: DatabaseTable { // MARK: - Marking @discardableResult - func mark(_ statuses: Set, _ statusKey: ArticleStatus.Key, _ flag: Bool, _ database: FMDatabase) -> Set? { + func mark(_ statuses: Set, _ statusKey: ArticleStatus.Key, _ flag: Bool, _ database: FMDatabase) -> Set { // Sets flag in both memory and in database. var updatedStatuses = Set() @@ -75,13 +75,11 @@ final class StatusesTable: DatabaseTable { updatedStatuses.insert(status) } - if updatedStatuses.isEmpty { - return nil - } let articleIDs = updatedStatuses.articleIDs() - - self.markArticleIDs(articleIDs, statusKey, flag, database) - + if !articleIDs.isEmpty { + markArticleIDs(articleIDs, statusKey, flag, database) + } + return updatedStatuses }