Fix deprecation warning.

This commit is contained in:
Brent Simmons 2024-04-22 17:58:04 -07:00
parent d52e53d2e8
commit c0619d2364

View File

@ -193,37 +193,81 @@ enum CloudKitAccountZoneError: LocalizedError {
public func findOrCreateAccount() async throws -> String { public func findOrCreateAccount() async throws -> String {
guard let database else { throw CloudKitAccountZoneError.unknown } try await withCheckedThrowingContinuation { continuation in
self.findOrCreateAccount { result in
switch result {
case .success(let externalID):
continuation.resume(returning: externalID)
case .failure(let error):
continuation.resume(throwing: error)
}
}
}
}
private func findOrCreateAccount(completion: @escaping @Sendable (Result<String, Error>) -> Void) {
guard let database else {
completion(.failure(CloudKitAccountZoneError.unknown))
return
}
let predicate = NSPredicate(format: "isAccount = \"1\"") let predicate = NSPredicate(format: "isAccount = \"1\"")
let ckQuery = CKQuery(recordType: CloudKitContainer.recordType, predicate: predicate) let ckQuery = CKQuery(recordType: CloudKitContainer.recordType, predicate: predicate)
do { database.fetch(withQuery: ckQuery, inZoneWith: zoneID) { result in
let records = try await database.perform(ckQuery, inZoneWith: zoneID)
if records.count > 0 {
return records[0].externalID
} else {
return try await createContainer(name: "Account", isAccount: true)
}
} catch { switch result {
switch CloudKitZoneResult.resolve(error) { case .success((let matchResults, _)):
case .retry(let timeToWait): for result in matchResults {
await delay(for: timeToWait) let (_, recordResult) = result
return try await findOrCreateAccount() switch recordResult {
case .zoneNotFound, .userDeletedZone: case .success(let record):
_ = try await createZoneRecord() completion(.success(record.externalID))
return try await findOrCreateAccount() return
default: case .failure(_):
return try await createContainer(name: "Account", isAccount: true) continue
}
}
// If no records in matchResults
completion(.failure(CloudKitAccountZoneError.unknown))
case .failure(let error):
switch CloudKitZoneResult.resolve(error) {
case .retry(let timeToWait):
self.retryIfPossible(after: timeToWait) {
self.findOrCreateAccount(completion: completion)
}
case .zoneNotFound, .userDeletedZone:
Task { @MainActor in
_ = try await self.createZoneRecord()
self.findOrCreateAccount(completion: completion)
}
default:
Task { @MainActor in
do {
let externalID = try await self.createContainer(name: "Account", isAccount: true)
completion(.success(externalID))
} catch {
completion(.failure(error))
}
}
}
} }
} }
} }
public func createFolder(name: String) async throws -> String { public func createFolder(name: String) async throws -> String {
return try await createContainer(name: name, isAccount: false) return try await createContainer(name: name, isAccount: false)