diff --git a/Account/Sources/Account/CloudKit/CloudKitAccountDelegate.swift b/Account/Sources/Account/CloudKit/CloudKitAccountDelegate.swift index c3db40cb5..65937c92d 100644 --- a/Account/Sources/Account/CloudKit/CloudKitAccountDelegate.swift +++ b/Account/Sources/Account/CloudKit/CloudKitAccountDelegate.swift @@ -414,13 +414,12 @@ enum CloudKitAccountDelegateError: LocalizedError { return SyncStatus(articleID: article.articleID, key: SyncStatus.Key(statusKey), flag: flag) } - self.database.insertStatuses(syncStatuses) { _ in - Task { @MainActor in - if let count = try? await self.database.selectPendingCount(), count > 100 { - self.sendArticleStatus(for: account, showProgress: false) { _ in } - } - completion(.success(())) + Task { @MainActor in + try? await self.database.insertStatuses(syncStatuses) + if let count = try? await self.database.selectPendingCount(), count > 100 { + self.sendArticleStatus(for: account, showProgress: false) { _ in } } + completion(.success(())) } case .failure(let error): completion(.failure(error)) @@ -769,7 +768,10 @@ private extension CloudKitAccountDelegate { let syncStatuses = articles.map { article in return SyncStatus(articleID: article.articleID, key: statusKey, flag: flag) } - database.insertStatuses(syncStatuses) { _ in + + Task { @MainActor in + + try? await self.database.insertStatuses(syncStatuses) completion() } } diff --git a/Account/Sources/Account/CloudKit/CloudKitArticlesZoneDelegate.swift b/Account/Sources/Account/CloudKit/CloudKitArticlesZoneDelegate.swift index bbfee35ef..fe082fa65 100644 --- a/Account/Sources/Account/CloudKit/CloudKitArticlesZoneDelegate.swift +++ b/Account/Sources/Account/CloudKit/CloudKitArticlesZoneDelegate.swift @@ -145,24 +145,23 @@ private extension CloudKitArticlesZoneDelegate { for (feedID, parsedItems) in feedIDsAndItems { group.enter() self.account?.update(feedID, with: parsedItems, deleteOlder: false) { result in - MainActor.assumeIsolated { + Task { @MainActor in switch result { case .success(let articleChanges): guard let deletes = articleChanges.deletedArticles, !deletes.isEmpty else { group.leave() return } + let syncStatuses = deletes.map { SyncStatus(articleID: $0.articleID, key: .deleted, flag: true) } - self.database.insertStatuses(syncStatuses) { _ in - group.leave() - } + try? await self.database.insertStatuses(syncStatuses) + group.leave() case .failure(let databaseError): errorOccurred = true os_log(.error, log: self.log, "Error occurred while storing articles: %@", databaseError.localizedDescription) group.leave() } } - group.leave() } } } diff --git a/Account/Sources/Account/Feedbin/FeedbinAccountDelegate.swift b/Account/Sources/Account/Feedbin/FeedbinAccountDelegate.swift index d913e6eff..89d67f0f1 100644 --- a/Account/Sources/Account/Feedbin/FeedbinAccountDelegate.swift +++ b/Account/Sources/Account/Feedbin/FeedbinAccountDelegate.swift @@ -563,16 +563,16 @@ final class FeedbinAccountDelegate: AccountDelegate { let syncStatuses = articles.map { article in return SyncStatus(articleID: article.articleID, key: SyncStatus.Key(statusKey), flag: flag) } - - self.database.insertStatuses(syncStatuses) { _ in - - Task { @MainActor in - if let count = try? await self.database.selectPendingCount(), count > 100 { - self.sendArticleStatus(for: account) { _ in } - } - completion(.success(())) + + Task { @MainActor in + try? await self.database.insertStatuses(syncStatuses) + + if let count = try? await self.database.selectPendingCount(), count > 100 { + self.sendArticleStatus(for: account) { _ in } } + completion(.success(())) } + case .failure(let error): completion(.failure(error)) } diff --git a/Account/Sources/Account/Feedly/FeedlyAccountDelegate.swift b/Account/Sources/Account/Feedly/FeedlyAccountDelegate.swift index 9cc3de1cd..b1e15228a 100644 --- a/Account/Sources/Account/Feedly/FeedlyAccountDelegate.swift +++ b/Account/Sources/Account/Feedly/FeedlyAccountDelegate.swift @@ -514,14 +514,14 @@ final class FeedlyAccountDelegate: AccountDelegate { return SyncStatus(articleID: article.articleID, key: SyncStatus.Key(statusKey), flag: flag) } - self.database.insertStatuses(syncStatuses) { _ in + Task { @MainActor in - Task { @MainActor in - if let count = try? await self.database.selectPendingCount(), count > 100 { - self.sendArticleStatus(for: account) { _ in } - } - completion(.success(())) + try? await self.database.insertStatuses(syncStatuses) + + if let count = try? await self.database.selectPendingCount(), count > 100 { + self.sendArticleStatus(for: account) { _ in } } + completion(.success(())) } case .failure(let error): completion(.failure(error)) diff --git a/Account/Sources/Account/NewsBlur/NewsBlurAccountDelegate.swift b/Account/Sources/Account/NewsBlur/NewsBlurAccountDelegate.swift index 58788c06c..5f91466b3 100644 --- a/Account/Sources/Account/NewsBlur/NewsBlurAccountDelegate.swift +++ b/Account/Sources/Account/NewsBlur/NewsBlurAccountDelegate.swift @@ -594,14 +594,14 @@ final class NewsBlurAccountDelegate: AccountDelegate { return SyncStatus(articleID: article.articleID, key: SyncStatus.Key(statusKey), flag: flag) } - self.database.insertStatuses(syncStatuses) { _ in + Task { @MainActor in - Task { @MainActor in - if let count = try? await self.database.selectPendingCount(), count > 100 { - self.sendArticleStatus(for: account) { _ in } - } - completion(.success(())) + try? await self.database.insertStatuses(syncStatuses) + + if let count = try? await self.database.selectPendingCount(), count > 100 { + self.sendArticleStatus(for: account) { _ in } } + completion(.success(())) } case .failure(let error): completion(.failure(error)) diff --git a/Account/Sources/Account/ReaderAPI/ReaderAPIAccountDelegate.swift b/Account/Sources/Account/ReaderAPI/ReaderAPIAccountDelegate.swift index 917b20dfb..817bccb97 100644 --- a/Account/Sources/Account/ReaderAPI/ReaderAPIAccountDelegate.swift +++ b/Account/Sources/Account/ReaderAPI/ReaderAPIAccountDelegate.swift @@ -619,22 +619,24 @@ final class ReaderAPIAccountDelegate: AccountDelegate { let syncStatuses = articles.map { article in return SyncStatus(articleID: article.articleID, key: SyncStatus.Key(statusKey), flag: flag) } - - self.database.insertStatuses(syncStatuses) { _ in - - Task { @MainActor in - if let count = try? await self.database.selectPendingCount(), count > 100 { - self.sendArticleStatus(for: account) { _ in } - } - completion(.success(())) + + Task { @MainActor in + + try? await self.database.insertStatuses(syncStatuses) + + if let count = try? await self.database.selectPendingCount(), count > 100 { + self.sendArticleStatus(for: account) { _ in } } + + completion(.success(())) } + case .failure(let error): completion(.failure(error)) } } } - + func accountDidInitialize(_ account: Account) { credentials = try? account.retrieveCredentials(type: .readerAPIKey) } diff --git a/SyncDatabase/Sources/SyncDatabase/SyncDatabase.swift b/SyncDatabase/Sources/SyncDatabase/SyncDatabase.swift index 6f9143c54..1e5d42412 100644 --- a/SyncDatabase/Sources/SyncDatabase/SyncDatabase.swift +++ b/SyncDatabase/Sources/SyncDatabase/SyncDatabase.swift @@ -122,18 +122,6 @@ public typealias SyncStatusArticleIDsCompletionBlock = @Sendable (SyncStatusArti public extension SyncDatabase { - nonisolated func insertStatuses(_ statuses: [SyncStatus], completion: @escaping DatabaseCompletionBlock) { - - Task { @MainActor in - do { - try await self.insertStatuses(statuses) - completion(nil) - } catch { - completion(DatabaseError.suspended) - } - } - } - nonisolated func selectForProcessing(limit: Int? = nil, completion: @escaping SyncStatusesCompletionBlock) { Task { @MainActor in