Delete last completion-based method in SyncDatabase.

This commit is contained in:
Brent Simmons 2024-03-25 22:47:43 -07:00
parent c50ac0c84b
commit 4a5cb237a0
6 changed files with 79 additions and 117 deletions

View File

@ -67,43 +67,39 @@ class CloudKitSendStatusOperation: MainThreadOperation {
}
private extension CloudKitSendStatusOperation {
func selectForProcessing() {
database.selectForProcessing(limit: blockSize) { result in
MainActor.assumeIsolated {
switch result {
case .success(let syncStatuses):
Task { @MainActor in
@MainActor func stopProcessing() {
if self.showProgress {
self.refreshProgress?.completeTask()
}
os_log(.debug, log: self.log, "Done sending article statuses.")
self.operationDelegate?.operationDidComplete(self)
}
guard syncStatuses.count > 0 else {
stopProcessing()
return
}
self.processStatuses(syncStatuses) { stop in
if stop {
stopProcessing()
} else {
self.selectForProcessing()
}
}
case .failure(let databaseError):
os_log(.error, log: self.log, "Send status error: %@.", databaseError.localizedDescription)
self.operationDelegate?.cancelOperation(self)
@MainActor func stopProcessing() {
if self.showProgress {
self.refreshProgress?.completeTask()
}
os_log(.debug, log: self.log, "Done sending article statuses.")
self.operationDelegate?.operationDidComplete(self)
}
do {
guard let syncStatuses = try await self.database.selectForProcessing(limit: blockSize), !syncStatuses.isEmpty else {
stopProcessing()
return
}
self.processStatuses(Array(syncStatuses)) { stop in
if stop {
stopProcessing()
} else {
self.selectForProcessing()
}
}
} catch {
os_log(.error, log: self.log, "Send status error: %@.", error.localizedDescription)
self.operationDelegate?.cancelOperation(self)
}
}
}
@MainActor func processStatuses(_ syncStatuses: [SyncStatus], completion: @escaping (Bool) -> Void) {
guard let account = account, let articlesZone = articlesZone else {
completion(true)

View File

@ -134,18 +134,21 @@ final class FeedbinAccountDelegate: AccountDelegate {
os_log(.debug, log: log, "Sending article statuses...")
database.selectForProcessing { result in
Task { @MainActor in
do {
let syncStatuses = (try await self.database.selectForProcessing()) ?? Set<SyncStatus>()
MainActor.assumeIsolated {
@MainActor func processStatuses(_ syncStatuses: [SyncStatus]) {
let createUnreadStatuses = syncStatuses.filter { $0.key == SyncStatus.Key.read && $0.flag == false }
let deleteUnreadStatuses = syncStatuses.filter { $0.key == SyncStatus.Key.read && $0.flag == true }
let createStarredStatuses = syncStatuses.filter { $0.key == SyncStatus.Key.starred && $0.flag == true }
let deleteStarredStatuses = syncStatuses.filter { $0.key == SyncStatus.Key.starred && $0.flag == false }
let group = DispatchGroup()
var errorOccurred = false
group.enter()
self.sendArticleStatuses(createUnreadStatuses, apiCall: self.caller.createUnreadEntries) { result in
group.leave()
@ -153,7 +156,7 @@ final class FeedbinAccountDelegate: AccountDelegate {
errorOccurred = true
}
}
group.enter()
self.sendArticleStatuses(deleteUnreadStatuses, apiCall: self.caller.deleteUnreadEntries) { result in
group.leave()
@ -161,7 +164,7 @@ final class FeedbinAccountDelegate: AccountDelegate {
errorOccurred = true
}
}
group.enter()
self.sendArticleStatuses(createStarredStatuses, apiCall: self.caller.createStarredEntries) { result in
group.leave()
@ -169,7 +172,7 @@ final class FeedbinAccountDelegate: AccountDelegate {
errorOccurred = true
}
}
group.enter()
self.sendArticleStatuses(deleteStarredStatuses, apiCall: self.caller.deleteStarredEntries) { result in
group.leave()
@ -177,7 +180,7 @@ final class FeedbinAccountDelegate: AccountDelegate {
errorOccurred = true
}
}
group.notify(queue: DispatchQueue.main) {
os_log(.debug, log: self.log, "Done sending article statuses.")
if errorOccurred {
@ -187,13 +190,11 @@ final class FeedbinAccountDelegate: AccountDelegate {
}
}
}
switch result {
case .success(let syncStatuses):
processStatuses(syncStatuses)
case .failure(let databaseError):
completion(.failure(databaseError))
}
processStatuses(Array(syncStatuses))
} catch {
completion(.failure(error))
}
}
}

View File

@ -28,19 +28,13 @@ final class FeedlySendArticleStatusesOperation: FeedlyOperation {
override func run() {
os_log(.debug, log: log, "Sending article statuses...")
database.selectForProcessing { result in
MainActor.assumeIsolated {
if self.isCanceled {
self.didFinish()
return
}
Task { @MainActor in
switch result {
case .success(let syncStatuses):
self.processStatuses(syncStatuses)
case .failure:
self.didFinish()
}
do {
let syncStatuses = (try await self.database.selectForProcessing()) ?? Set<SyncStatus>()
self.processStatuses(Array(syncStatuses))
} catch {
self.didFinish()
}
}
}

View File

@ -134,10 +134,11 @@ final class NewsBlurAccountDelegate: AccountDelegate {
func sendArticleStatus(for account: Account, completion: @escaping (Result<Void, Error>) -> ()) {
os_log(.debug, log: log, "Sending story statuses...")
database.selectForProcessing { result in
Task { @MainActor in
do {
let syncStatuses = (try await self.database.selectForProcessing()) ?? Set<SyncStatus>()
MainActor.assumeIsolated {
@MainActor func processStatuses(_ syncStatuses: [SyncStatus]) {
let createUnreadStatuses = syncStatuses.filter {
$0.key == SyncStatus.Key.read && $0.flag == false
@ -151,10 +152,10 @@ final class NewsBlurAccountDelegate: AccountDelegate {
let deleteStarredStatuses = syncStatuses.filter {
$0.key == SyncStatus.Key.starred && $0.flag == false
}
let group = DispatchGroup()
var errorOccurred = false
group.enter()
self.sendStoryStatuses(createUnreadStatuses, throttle: true, apiCall: self.caller.markAsUnread) { result in
group.leave()
@ -162,7 +163,7 @@ final class NewsBlurAccountDelegate: AccountDelegate {
errorOccurred = true
}
}
group.enter()
self.sendStoryStatuses(deleteUnreadStatuses, throttle: false, apiCall: self.caller.markAsRead) { result in
group.leave()
@ -170,7 +171,7 @@ final class NewsBlurAccountDelegate: AccountDelegate {
errorOccurred = true
}
}
group.enter()
self.sendStoryStatuses(createStarredStatuses, throttle: true, apiCall: self.caller.star) { result in
group.leave()
@ -178,7 +179,7 @@ final class NewsBlurAccountDelegate: AccountDelegate {
errorOccurred = true
}
}
group.enter()
self.sendStoryStatuses(deleteStarredStatuses, throttle: true, apiCall: self.caller.unstar) { result in
group.leave()
@ -186,7 +187,7 @@ final class NewsBlurAccountDelegate: AccountDelegate {
errorOccurred = true
}
}
group.notify(queue: DispatchQueue.main) {
os_log(.debug, log: self.log, "Done sending article statuses.")
if errorOccurred {
@ -196,13 +197,10 @@ final class NewsBlurAccountDelegate: AccountDelegate {
}
}
}
switch result {
case .success(let syncStatuses):
processStatuses(syncStatuses)
case .failure(let databaseError):
completion(.failure(databaseError))
}
processStatuses(Array(syncStatuses))
} catch {
completion(.failure(error))
}
}
}

View File

@ -199,52 +199,53 @@ final class ReaderAPIAccountDelegate: AccountDelegate {
}
func sendArticleStatus(for account: Account, completion: @escaping ((Result<Void, Error>) -> Void)) {
os_log(.debug, log: log, "Sending article statuses...")
database.selectForProcessing { result in
Task { @MainActor in
do {
let syncStatuses = (try await self.database.selectForProcessing()) ?? Set<SyncStatus>()
MainActor.assumeIsolated {
@MainActor func processStatuses(_ syncStatuses: [SyncStatus]) {
let createUnreadStatuses = syncStatuses.filter { $0.key == SyncStatus.Key.read && $0.flag == false }
let deleteUnreadStatuses = syncStatuses.filter { $0.key == SyncStatus.Key.read && $0.flag == true }
let createStarredStatuses = syncStatuses.filter { $0.key == SyncStatus.Key.starred && $0.flag == true }
let deleteStarredStatuses = syncStatuses.filter { $0.key == SyncStatus.Key.starred && $0.flag == false }
let group = DispatchGroup()
group.enter()
self.sendArticleStatuses(createUnreadStatuses, apiCall: self.caller.createUnreadEntries) {
group.leave()
}
group.enter()
self.sendArticleStatuses(deleteUnreadStatuses, apiCall: self.caller.deleteUnreadEntries) {
group.leave()
}
group.enter()
self.sendArticleStatuses(createStarredStatuses, apiCall: self.caller.createStarredEntries) {
group.leave()
}
group.enter()
self.sendArticleStatuses(deleteStarredStatuses, apiCall: self.caller.deleteStarredEntries) {
group.leave()
}
group.notify(queue: DispatchQueue.main) {
os_log(.debug, log: self.log, "Done sending article statuses.")
completion(.success(()))
}
}
switch result {
case .success(let syncStatuses):
processStatuses(syncStatuses)
case .failure(let databaseError):
completion(.failure(databaseError))
}
processStatuses(Array(syncStatuses))
} catch {
completion(.failure(error))
}
}
}

View File

@ -110,34 +110,6 @@ public actor SyncDatabase {
}
}
// MARK: - Compatibility
// Use the below until switching to the async version of the API.
public typealias SyncStatusesResult = Result<Array<SyncStatus>, DatabaseError>
public typealias SyncStatusesCompletionBlock = @Sendable (SyncStatusesResult) -> Void
public typealias SyncStatusArticleIDsResult = Result<Set<String>, DatabaseError>
public typealias SyncStatusArticleIDsCompletionBlock = @Sendable (SyncStatusArticleIDsResult) -> Void
public extension SyncDatabase {
nonisolated func selectForProcessing(limit: Int? = nil, completion: @escaping SyncStatusesCompletionBlock) {
Task { @MainActor in
do {
if let syncStatuses = try await self.selectForProcessing(limit: limit) {
completion(.success(Array(syncStatuses)))
} else {
completion(.success([SyncStatus]()))
}
} catch {
completion(.failure(DatabaseError.suspended))
}
}
}
}
private extension SyncDatabase {
static let creationStatements = """