在 iOS 15 的 Xcode 13 beta 中,我收到一条消息,指出perform(_:inZoneWith:completionHandler:)
(CloudKit) 在 iOS 15 中已弃用并重命名为fetchRecords(matching:inZoneWith:desiredKeys:resultsLimit:completionHandler:)
但是...
Apple Docs 网站未声明此方法已弃用:https ://developer.apple.com/documentation/cloudkit/ckdatabase/1449127-perform
Apple 正在展示对 iOS 15 的其他弃用(另一种方法):https ://developer.apple.com/documentation/cloudkit/ckdatabase/3794331-records/
fetchRecords(matching:inZoneWith:desiredKeys:resultsLimit:completionHandler:)
似乎不存在..尚未..(Value of type 'CKDatabase' has no member 'fetchRecords'
)
那么,这只是一个不正确的消息,因为它的测试版?我应该担心重写使用的函数perform(_:inZoneWith:completionHandler:)
吗?
这是我的函数,我尝试将其调整为 fetchRecords,但它不存在。我尝试适应它,fetch(withQuery:completionHandler:
但我有点迷失了让它工作..
(这个函数只是从 iCloud 私有数据库中删除记录):
let container = CKContainer(identifier: "MyContainerNameHere")
let recordType = "DBName"
//delete all saved icloud records
let query = CKQuery(recordType: recordType, predicate: NSPredicate(value: true))
container.privateCloudDatabase.perform(query, inZoneWith: nil) { (rec, err) in
if let err = err {
print(err.localizedDescription)
completion(.failure(err))
return
}
guard let rec = rec else {
completion(.failure(CloudKitHelperError.castFailure))
return
}
for record in rec {
container.privateCloudDatabase.delete(withRecordID: record.recordID) { (recordId, err) in
if let err = err {
print(err.localizedDescription)
completion(.failure(err))
return
}
guard recordId != nil else {
completion(.failure(CloudKitHelperError.recordIDFailure))
return
}
}
}
}
任何见解表示赞赏..
谢谢
更新 我会说,是的,这似乎是一个错误,或者至少是一个过早的消息,但是,在为 async/await 重写代码之后,它更清晰,更易于阅读。对于那些努力解决这个问题的人,这里是上面转换为 Async/Await 的代码示例:
@MainActor func newDeleteCloudKit() async throws {
let container = CKContainer(identifier: "MyContainerNameHere")
let recordType = "DBName"
let query = CKQuery(recordType: recordType, predicate: NSPredicate(value: true))
let result = try await container.privateCloudDatabase.records(matching: query)
for record in result.0 {
try await container.privateCloudDatabase.deleteRecord(withID: record.0)
}
}