From 24c8bdcc1a31c25f79eeff1e77a791bbcfcba6b8 Mon Sep 17 00:00:00 2001 From: Brent Simmons Date: Fri, 19 Apr 2024 19:20:40 -0700 Subject: [PATCH] Convert delete(subscriptionID) to async await. --- .../Sources/CloudKitExtras/CloudKitZone.swift | 49 +++++-------------- 1 file changed, 11 insertions(+), 38 deletions(-) diff --git a/CloudKitExtras/Sources/CloudKitExtras/CloudKitZone.swift b/CloudKitExtras/Sources/CloudKitExtras/CloudKitZone.swift index 9feae2f32..88e9aa87d 100644 --- a/CloudKitExtras/Sources/CloudKitExtras/CloudKitZone.swift +++ b/CloudKitExtras/Sources/CloudKitExtras/CloudKitZone.swift @@ -823,48 +823,21 @@ public protocol CloudKitZone: AnyObject { /// Delete a CKSubscription func delete(subscriptionID: String) async throws { - try await withCheckedThrowingContinuation { continuation in - self.delete(subscriptionID: subscriptionID) { result in - switch result { - case .success: - continuation.resume() - case .failure(let error): - continuation.resume(throwing: error) - } - } - } - } + guard let database else { throw CloudKitZoneError.unknown } - /// Delete a CKSubscription - func delete(subscriptionID: String, completion: @escaping @Sendable (Result) -> Void) { + do { + _ = try await database.deleteSubscription(withID: subscriptionID) + } catch { - guard let database else { - completion(.failure(CloudKitZoneError.unknown)) - return - } + switch CloudKitZoneResult.resolve(error) { - database.delete(withSubscriptionID: subscriptionID) { [weak self] _, error in + case .retry(let timeToWait): + os_log(.error, log: self.log, "%@ zone delete subscription retry in %f seconds.", self.zoneID.zoneName, timeToWait) + await self.delay(for: timeToWait) + try await delete(subscriptionID: subscriptionID) - guard let self else { - completion(.failure(CloudKitZoneError.unknown)) - return - } - - Task { @MainActor in - - switch CloudKitZoneResult.resolve(error) { - - case .success: - completion(.success(())) - - case .retry(let timeToWait): - os_log(.error, log: self.log, "%@ zone delete subscription retry in %f seconds.", self.zoneID.zoneName, timeToWait) - await self.delay(for: timeToWait) - self.delete(subscriptionID: subscriptionID, completion: completion) - - default: - completion(.failure(CloudKitError(error!))) - } + default: + throw CloudKitError(error) } } }