From ba70b8d4b7a01af19c3b7e1e23d20970bb13fd4d Mon Sep 17 00:00:00 2001 From: Brent Simmons Date: Sun, 21 Apr 2024 12:12:19 -0700 Subject: [PATCH] Convert refreshArticles to async await. --- .../CloudKitReceiveStatusOperation.swift | 17 +++++----- .../CloudKitSync/CloudKitArticlesZone.swift | 34 +++++++------------ 2 files changed, 21 insertions(+), 30 deletions(-) diff --git a/Account/Sources/Account/CloudKit/CloudKitReceiveStatusOperation.swift b/Account/Sources/Account/CloudKit/CloudKitReceiveStatusOperation.swift index b911ea997..2ef0f0c04 100644 --- a/Account/Sources/Account/CloudKit/CloudKitReceiveStatusOperation.swift +++ b/Account/Sources/Account/CloudKit/CloudKitReceiveStatusOperation.swift @@ -29,23 +29,24 @@ final class CloudKitReceiveStatusOperation: MainThreadOperation { } @MainActor func run() { - guard let articlesZone = articlesZone else { - self.operationDelegate?.operationDidComplete(self) + + guard let articlesZone else { + operationDelegate?.operationDidComplete(self) return } os_log(.debug, log: log, "Refreshing article statuses...") - articlesZone.refreshArticles() { result in - os_log(.debug, log: self.log, "Done refreshing article statuses.") - switch result { - case .success: + Task { @MainActor in + do { + try await articlesZone.refreshArticles() self.operationDelegate?.operationDidComplete(self) - case .failure(let error): + } catch { os_log(.error, log: self.log, "Receive status error: %@.", error.localizedDescription) self.operationDelegate?.cancelOperation(self) } + + os_log(.debug, log: self.log, "Done refreshing article statuses.") } } - } diff --git a/CloudKitSync/Sources/CloudKitSync/CloudKitArticlesZone.swift b/CloudKitSync/Sources/CloudKitSync/CloudKitArticlesZone.swift index 0c00d01a9..40ddd5897 100644 --- a/CloudKitSync/Sources/CloudKitSync/CloudKitArticlesZone.swift +++ b/CloudKitSync/Sources/CloudKitSync/CloudKitArticlesZone.swift @@ -22,9 +22,9 @@ public protocol CloudKitFeedInfoDelegate { @MainActor func feedURL(article: Article) -> String? } -public final class CloudKitArticlesZone: CloudKitZone { +@MainActor public final class CloudKitArticlesZone: CloudKitZone { - public var zoneID: CKRecordZone.ID + public let zoneID: CKRecordZone.ID public var log = OSLog(subsystem: Bundle.main.bundleIdentifier!, category: "CloudKit") @@ -72,26 +72,17 @@ public final class CloudKitArticlesZone: CloudKitZone { migrateChangeToken() } - @MainActor public func refreshArticles(completion: @escaping ((Result) -> Void)) { - fetchChangesInZone() { result in - switch result { - case .success: - completion(.success(())) - case .failure(let error): - if case CloudKitZoneError.userDeletedZone = error { + @MainActor public func refreshArticles() async throws { - Task { @MainActor in - do { - _ = try await self.createZoneRecord() - self.refreshArticles(completion: completion) - } catch { - completion(.failure(error)) - } - } + do { + try await fetchChangesInZone() - } else { - completion(.failure(error)) - } + } catch { + if case CloudKitZoneError.userDeletedZone = error { + _ = try await self.createZoneRecord() + try await refreshArticles() + } else { + throw error } } } @@ -247,7 +238,7 @@ private extension CloudKitArticlesZone { return record } - func compressArticleRecords(_ records: [CKRecord]) -> [CKRecord] { + nonisolated func compressArticleRecords(_ records: [CKRecord]) -> [CKRecord] { var result = [CKRecord]() for record in records { @@ -277,5 +268,4 @@ private extension CloudKitArticlesZone { return result } - }