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:
@@ -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<String>, statusKey: ArticleStatus.Key, flag: Bool, completion: DatabaseCompletionBlock? = nil) {
|
||||
|
||||
@@ -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<String>) async throws {
|
||||
public func createStatusesIfNeeded(articleIDs: Set<String>) async throws -> [String: ArticleStatus] {
|
||||
try await articlesTable.createStatusesIfNeeded(articleIDs)
|
||||
}
|
||||
|
||||
|
||||
@@ -493,37 +493,6 @@ final class ArticlesTable: DatabaseTable {
|
||||
}
|
||||
}
|
||||
|
||||
func fetchStarredAndUnreadCount(_ feedIDs: Set<String>, _ 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<String> {
|
||||
@@ -574,30 +543,14 @@ final class ArticlesTable: DatabaseTable {
|
||||
}
|
||||
}
|
||||
|
||||
func mark(_ articleIDs: Set<String>, _ 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<String>) async throws {
|
||||
func createStatusesIfNeeded(_ articleIDs: Set<String>) 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<Article> {
|
||||
var cachedArticles = Set<Article>()
|
||||
var fetchedArticles = Set<Article>()
|
||||
|
||||
Reference in New Issue
Block a user