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.
/// All created statuses will be marked as read and not starred.
/// Sends a .StatusesDidChange notification.
func createStatusesIfNeeded(articleIDs: Set<String>, completion: DatabaseCompletionBlock? = nil) {
guard !articleIDs.isEmpty else {
completion?(nil)
return
}
database.createStatusesIfNeeded(articleIDs: articleIDs) { error in
func createStatusesIfNeeded(articleIDs: Set<String>) async throws {
MainActor.assumeIsolated {
if let error = error {
completion?(error)
guard !articleIDs.isEmpty else {
return
}
self.noteStatusesForArticleIDsDidChange(articleIDs)
completion?(nil)
}
}
try await database.createStatusesIfNeeded(articleIDs: articleIDs)
noteStatusesForArticleIDsDidChange(articleIDs)
}
/// Mark articleIDs statuses based on statusKey and flag.

View File

@ -50,24 +50,23 @@ class FeedlyIngestStreamArticleIdsOperation: FeedlyOperation {
}
switch result {
case .success(let streamIds):
account.createStatusesIfNeeded(articleIDs: Set(streamIds.ids)) { databaseError in
case .success(let streamIDs):
MainActor.assumeIsolated {
if let error = databaseError {
self.didFinish(with: error)
return
}
Task { @MainActor in
do {
try await account.createStatusesIfNeeded(articleIDs: Set(streamIDs.ids))
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)
self.didFinish()
return
}
self.getStreamIds(continuation)
} catch {
self.didFinish(with: error)
return
}
}
case .failure(let 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) {
Task { @MainActor in