Convert createStatusesIfNeeded to async await.

This commit is contained in:
Brent Simmons 2024-04-04 17:37:13 -07:00
parent 1824c15ddc
commit e523e06efe
3 changed files with 13 additions and 36 deletions

View File

@ -880,22 +880,14 @@ public enum FetchType {
/// Make sure statuses exist. Any existing statuses wont be touched. /// Make sure statuses exist. Any existing statuses wont be touched.
/// All created statuses will be marked as read and not starred. /// All created statuses will be marked as read and not starred.
/// Sends a .StatusesDidChange notification. /// Sends a .StatusesDidChange notification.
func createStatusesIfNeeded(articleIDs: Set<String>, completion: DatabaseCompletionBlock? = nil) { func createStatusesIfNeeded(articleIDs: Set<String>) async throws {
guard !articleIDs.isEmpty else { guard !articleIDs.isEmpty else {
completion?(nil)
return return
} }
database.createStatusesIfNeeded(articleIDs: articleIDs) { error in
MainActor.assumeIsolated { try await database.createStatusesIfNeeded(articleIDs: articleIDs)
if let error = error { noteStatusesForArticleIDsDidChange(articleIDs)
completion?(error)
return
}
self.noteStatusesForArticleIDsDidChange(articleIDs)
completion?(nil)
}
}
} }
/// Mark articleIDs statuses based on statusKey and flag. /// Mark articleIDs statuses based on statusKey and flag.

View File

@ -48,26 +48,25 @@ class FeedlyIngestStreamArticleIdsOperation: FeedlyOperation {
didFinish() didFinish()
return return
} }
switch result { switch result {
case .success(let streamIds): case .success(let streamIDs):
account.createStatusesIfNeeded(articleIDs: Set(streamIds.ids)) { databaseError in
MainActor.assumeIsolated { Task { @MainActor in
if let error = databaseError { do {
self.didFinish(with: error) try await account.createStatusesIfNeeded(articleIDs: Set(streamIDs.ids))
return
}
guard let continuation = streamIds.continuation else { guard let continuation = streamIDs.continuation else {
os_log(.debug, log: self.log, "Reached end of stream for %@", self.resource.id) os_log(.debug, log: self.log, "Reached end of stream for %@", self.resource.id)
self.didFinish() self.didFinish()
return return
} }
self.getStreamIds(continuation) self.getStreamIds(continuation)
} catch {
self.didFinish(with: error)
return
} }
} }
case .failure(let error): case .failure(let error):
didFinish(with: error) didFinish(with: error)

View File

@ -95,20 +95,6 @@ public extension ArticlesDatabase {
} }
} }
/// Create statuses for specified articleIDs. For existing statuses, dont do anything.
/// For newly-created statuses, mark them as read and not-starred.
nonisolated func createStatusesIfNeeded(articleIDs: Set<String>, completion: @escaping DatabaseCompletionBlock) {
Task {
do {
try await createStatusesIfNeeded(articleIDs: articleIDs)
callDatabaseCompletion(completion)
} catch {
callDatabaseCompletion(completion, .suspended)
}
}
}
nonisolated private func callUpdateArticlesCompletion(_ completion: @escaping UpdateArticlesCompletionBlock, _ result: UpdateArticlesResult) { nonisolated private func callUpdateArticlesCompletion(_ completion: @escaping UpdateArticlesCompletionBlock, _ result: UpdateArticlesResult) {
Task { @MainActor in Task { @MainActor in