mirror of
https://github.com/Ranchero-Software/NetNewsWire
synced 2025-08-12 06:26:36 +00:00
Continue converting ArticlesDatabase to async/await.
This commit is contained in:
@@ -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))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -239,8 +239,8 @@ public typealias ArticleStatusesResultBlock = (ArticleStatusesResult) -> Void
|
||||
try await articlesTable.fetchArticleIDsForStatusesWithoutArticlesNewerThanCutoffDate()
|
||||
}
|
||||
|
||||
public func mark(_ articles: Set<Article>, statusKey: ArticleStatus.Key, flag: Bool, completion: @escaping ArticleStatusesResultBlock) {
|
||||
return articlesTable.mark(articles, statusKey, flag, completion)
|
||||
public func mark(_ articles: Set<Article>, statusKey: ArticleStatus.Key, flag: Bool) async throws -> Set<ArticleStatus> {
|
||||
try await articlesTable.mark(articles, statusKey, flag)
|
||||
}
|
||||
|
||||
public func markArticleIDs(_ articleIDs: Set<String>, statusKey: ArticleStatus.Key, flag: Bool) async throws {
|
||||
|
||||
@@ -542,17 +542,16 @@ final class ArticlesTable: DatabaseTable {
|
||||
try await statusesTable.fetchArticleIDsForStatusesWithoutArticlesNewerThan(articleCutoffDate)
|
||||
}
|
||||
|
||||
func mark(_ articles: Set<Article>, _ 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<ArticleStatus>()))
|
||||
}
|
||||
case .failure(let databaseError):
|
||||
DispatchQueue.main.async {
|
||||
completion(.failure(databaseError))
|
||||
func mark(_ articles: Set<Article>, _ statusKey: ArticleStatus.Key, _ flag: Bool) async throws -> Set<ArticleStatus> {
|
||||
|
||||
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)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -62,7 +62,7 @@ final class StatusesTable: DatabaseTable {
|
||||
// MARK: - Marking
|
||||
|
||||
@discardableResult
|
||||
func mark(_ statuses: Set<ArticleStatus>, _ statusKey: ArticleStatus.Key, _ flag: Bool, _ database: FMDatabase) -> Set<ArticleStatus>? {
|
||||
func mark(_ statuses: Set<ArticleStatus>, _ statusKey: ArticleStatus.Key, _ flag: Bool, _ database: FMDatabase) -> Set<ArticleStatus> {
|
||||
// Sets flag in both memory and in database.
|
||||
|
||||
var updatedStatuses = Set<ArticleStatus>()
|
||||
@@ -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
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user