mirror of
https://github.com/Ranchero-Software/NetNewsWire
synced 2026-12-01 01:40:22 +00:00
Add pageHandler callback for paginated queries.
This commit is contained in:
1 parent
3e19c1a71d
commit
76610c751a
1 file changed
+47
-11
@@ -10,6 +10,8 @@ import CloudKit
|
||||
import os.log
|
||||
import RSCore
|
||||
|
||||
public typealias CloudKitQueryPageHandler = @MainActor @Sendable ([CKRecord]) async -> Void
|
||||
|
||||
public enum CloudKitZoneError: LocalizedError, Sendable {
|
||||
case userDeletedZone
|
||||
case corruptAccount
|
||||
@@ -18,7 +20,7 @@ public enum CloudKitZoneError: LocalizedError, Sendable {
|
||||
public var errorDescription: String? {
|
||||
switch self {
|
||||
case .userDeletedZone:
|
||||
return NSLocalizedString("The iCloud data was deleted. Please remove the application iCloud account and add it again to continue using the application's iCloud support.", comment: "User deleted zone.")
|
||||
return NSLocalizedString("The iCloud data was deleted. Please remove the application iCloud account and add it again to continue using the application's iCloud support.", comment: "User deleted zone.")
|
||||
case .corruptAccount:
|
||||
return NSLocalizedString("There is an unrecoverable problem with your application iCloud account. Please make sure you have iCloud and iCloud Drive enabled in System Preferences. Then remove the application iCloud account and add it again.", comment: "Corrupt account.")
|
||||
default:
|
||||
@@ -175,14 +177,14 @@ public extension CloudKitZone {
|
||||
}
|
||||
|
||||
/// Issue a CKQuery and return the resulting CKRecords.
|
||||
func query(_ ckQuery: CKQuery, desiredKeys: [String]? = nil, completion: @escaping (Result<[CKRecord], Error>) -> Void) {
|
||||
func query(_ ckQuery: CKQuery, desiredKeys: [String]? = nil, pageHandler: CloudKitQueryPageHandler? = nil, completion: @escaping (Result<[CKRecord], Error>) -> Void) {
|
||||
Self.logger.debug("CloudKitZone: query ckQuery \(self.zoneID.zoneName, privacy: .public)")
|
||||
var records = [CKRecord]()
|
||||
|
||||
let op = CKQueryOperation(query: ckQuery)
|
||||
op.qualityOfService = Self.qualityOfService
|
||||
|
||||
if let desiredKeys = desiredKeys {
|
||||
if let desiredKeys {
|
||||
op.desiredKeys = desiredKeys
|
||||
}
|
||||
|
||||
@@ -201,10 +203,12 @@ public extension CloudKitZone {
|
||||
return
|
||||
}
|
||||
|
||||
await pageHandler?(records)
|
||||
|
||||
switch result {
|
||||
case .success(let cursor):
|
||||
if let cursor {
|
||||
self.query(cursor: cursor, desiredKeys: desiredKeys, carriedRecords: records, completion: completion)
|
||||
self.query(cursor: cursor, desiredKeys: desiredKeys, carriedRecords: records, pageHandler: pageHandler, completion: completion)
|
||||
} else {
|
||||
completion(.success(records))
|
||||
}
|
||||
@@ -216,7 +220,7 @@ public extension CloudKitZone {
|
||||
self.createZoneRecord { result in
|
||||
switch result {
|
||||
case .success:
|
||||
self.query(ckQuery, desiredKeys: desiredKeys, completion: completion)
|
||||
self.query(ckQuery, desiredKeys: desiredKeys, pageHandler: pageHandler, completion: completion)
|
||||
case .failure(let error):
|
||||
completion(.failure(error))
|
||||
}
|
||||
@@ -224,7 +228,7 @@ public extension CloudKitZone {
|
||||
case .retry(let timeToWait):
|
||||
Self.logger.debug("CloudKitZone: \(self.zoneID.zoneName, privacy: .public) zone query retry in \(timeToWait) seconds")
|
||||
await delaySeconds(timeToWait)
|
||||
self.query(ckQuery, desiredKeys: desiredKeys, completion: completion)
|
||||
self.query(ckQuery, desiredKeys: desiredKeys, pageHandler: pageHandler, completion: completion)
|
||||
case .userDeletedZone:
|
||||
completion(.failure(CloudKitZoneError.userDeletedZone))
|
||||
default:
|
||||
@@ -238,14 +242,14 @@ public extension CloudKitZone {
|
||||
}
|
||||
|
||||
/// Query CKRecords using a CKQuery Cursor
|
||||
func query(cursor: CKQueryOperation.Cursor, desiredKeys: [String]? = nil, carriedRecords: [CKRecord], completion: @escaping (Result<[CKRecord], Error>) -> Void) {
|
||||
func query(cursor: CKQueryOperation.Cursor, desiredKeys: [String]? = nil, carriedRecords: [CKRecord], pageHandler: CloudKitQueryPageHandler? = nil, completion: @escaping (Result<[CKRecord], Error>) -> Void) {
|
||||
Self.logger.debug("CloudKitZone: query cursor \(self.zoneID.zoneName, privacy: .public)")
|
||||
var records = carriedRecords
|
||||
|
||||
let op = CKQueryOperation(cursor: cursor)
|
||||
op.qualityOfService = Self.qualityOfService
|
||||
|
||||
if let desiredKeys = desiredKeys {
|
||||
if let desiredKeys {
|
||||
op.desiredKeys = desiredKeys
|
||||
}
|
||||
|
||||
@@ -264,10 +268,12 @@ public extension CloudKitZone {
|
||||
return
|
||||
}
|
||||
|
||||
await pageHandler?(records)
|
||||
|
||||
switch result {
|
||||
case .success(let newCursor):
|
||||
if let newCursor {
|
||||
self.query(cursor: newCursor, desiredKeys: desiredKeys, carriedRecords: records, completion: completion)
|
||||
self.query(cursor: newCursor, desiredKeys: desiredKeys, carriedRecords: records, pageHandler: pageHandler, completion: completion)
|
||||
} else {
|
||||
completion(.success(records))
|
||||
}
|
||||
@@ -279,7 +285,7 @@ public extension CloudKitZone {
|
||||
self.createZoneRecord { result in
|
||||
switch result {
|
||||
case .success:
|
||||
self.query(cursor: cursor, desiredKeys: desiredKeys, carriedRecords: records, completion: completion)
|
||||
self.query(cursor: cursor, desiredKeys: desiredKeys, carriedRecords: records, pageHandler: pageHandler, completion: completion)
|
||||
case .failure(let error):
|
||||
completion(.failure(error))
|
||||
}
|
||||
@@ -287,7 +293,7 @@ public extension CloudKitZone {
|
||||
case .retry(let timeToWait):
|
||||
Self.logger.debug("CloudKitZone: query cursor \(self.zoneID.zoneName, privacy: .public) zone query retry in \(timeToWait) seconds")
|
||||
await delaySeconds(timeToWait)
|
||||
self.query(cursor: cursor, desiredKeys: desiredKeys, carriedRecords: records, completion: completion)
|
||||
self.query(cursor: cursor, desiredKeys: desiredKeys, carriedRecords: records, pageHandler: pageHandler, completion: completion)
|
||||
case .userDeletedZone:
|
||||
completion(.failure(CloudKitZoneError.userDeletedZone))
|
||||
default:
|
||||
@@ -834,6 +840,36 @@ public extension CloudKitZone {
|
||||
}
|
||||
}
|
||||
|
||||
func queryPaginated(_ ckQuery: CKQuery, desiredKeys: [String]? = nil, pageHandler: @escaping @MainActor @Sendable ([CKRecord]) async throws -> Void) async throws {
|
||||
nonisolated(unsafe) var pageError: Error?
|
||||
|
||||
let wrappedHandler: CloudKitQueryPageHandler = { records in
|
||||
guard pageError == nil else {
|
||||
return
|
||||
}
|
||||
do {
|
||||
try await pageHandler(records)
|
||||
} catch {
|
||||
pageError = error
|
||||
}
|
||||
}
|
||||
|
||||
let _ = try await withCheckedThrowingContinuation { (continuation: CheckedContinuation<[CKRecord], Error>) in
|
||||
query(ckQuery, desiredKeys: desiredKeys, pageHandler: wrappedHandler, completion: { result in
|
||||
switch result {
|
||||
case .success(let records):
|
||||
if let pageError {
|
||||
continuation.resume(throwing: pageError)
|
||||
} else {
|
||||
continuation.resume(returning: records)
|
||||
}
|
||||
case .failure(let error):
|
||||
continuation.resume(throwing: pageError ?? error)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func fetch(externalID: String?) async throws -> CKRecord {
|
||||
try await withCheckedThrowingContinuation { continuation in
|
||||
fetch(externalID: externalID) { result in
|
||||
|
||||
Reference in new issue
Block a user