我有用于从CloudKit的私有数据库中批量删除记录的代码,但是它不起作用。 (我还注意到我保存的记录没有保留。)stack overflow上的类似帖子都没有解决此问题。
似乎应该删除代码的某些记录实际上已删除,但有些则没有。当我再次运行代码时,仍然存在记录,但是记录数比以前少了。
这是我的代码:
class AppDelegate: UIResponder, UIApplicationDelegate, UISplitViewControllerDelegate {
var window: UIWindow?
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
let splitViewController = window!.rootViewController as! UISplitViewController
let navigationController = splitViewController.viewControllers[splitViewController.viewControllers.count-1] as! UINavigationController
navigationController.topViewController!.navigationItem.leftBarButtonItem = splitViewController.displayModeButtonItem
splitViewController.delegate = self
deleteRecords()
return true
}
}
let privateDatabase = CKContainer.default().privateCloudDatabase
func deleteRecords() {
print("deleteRecords()")
let predicate = NSPredicate(value: true)
let query = CKQuery(recordType: DatabaseNameStrings.recordTypeAffirmation, predicate: predicate)
privateDatabase.perform(query, inZoneWith: nil) {
(records: [CKRecord]?, error: Error?) in
if error != nil {
print(error as Any)
} else {
if let records = records {
print("records.count=", records.count)
let recordIDsToDelete = records.map { $0.recordID }
print("recordIDsToDelete:")
print(recordIDsToDelete)
let operation = CKModifyRecordsOperation(recordsToSave: nil, recordIDsToDelete: recordIDsToDelete)
operation.modifyRecordsCompletionBlock = { savedRecords, deletedRecordIDs, error in
if error == nil {
print("Batch delete records!")
print("number of records deleted:", deletedRecordIDs?.count as Any)
printNumberOfRecords()
} else {
print(error as Any)
}
}
privateDatabase.add(operation)
}
}
}
}
func printNumberOfRecords() {
let predicate = NSPredicate(value: true)
let query = CKQuery(recordType: DatabaseNameStrings.recordTypeAffirmation, predicate: predicate)
privateDatabase.perform(query, inZoneWith: nil) {
(records: [CKRecord]?, error: Error?) in
if error != nil {
print(error as Any)
} else {
if let records = records {
print("Number of records in CloudKit=", records.count)
}
}
}
}
这是代码的第一次运行在调试窗口中的输出:
deleteRecords()
records.count= 93
recordIDsToDelete:
[<CKRecordID: 0x280bbcb00; recordName=B33A3F23-23D3-44C6-AEBC-86DD718DBB62, zoneID=...>, ... ]
Batch delete records!
number of records deleted: Optional(93)
Number of records in CloudKit= 67
这是第二次运行代码时在调试窗口中的输出:
deleteRecords()
records.count= 92
recordIDsToDelete:
[<CKRecordID: 0x280080d00; recordName=BBA5B236-A036-4AC9-82E1-165D3B003E23, zoneID=...>, ... ]
Batch delete records!
number of records deleted: Optional(92)
Number of records in CloudKit= 52
当我使用此代码而不是deleteRecords()...
func deleteRecordsOneAtATime() {
print("deleteRecordsOneAtATime()")
let predicate = NSPredicate(value: true)
let query = CKQuery(recordType: DatabaseNameStrings.recordTypeAffirmation, predicate: predicate)
privateDatabase.perform(query, inZoneWith: nil) {
(records: [CKRecord]?, error: Error?) in
if error != nil {
print(error as Any)
} else {
if let records = records {
print("records.count=", records.count)
let recordIDsToDelete = records.map { $0.recordID }
print("recordIDsToDelete:")
print(recordIDsToDelete)
for recordID in recordIDsToDelete {
privateDatabase.delete(withRecordID: recordID) {
(localRecordID: CKRecord.ID?, error: Error?) in
if error != nil {
print("error:\n", error as Any)
} else {
if localRecordID != nil {
print("localRecordID:", localRecordID as Any)
}
}
}
}
}
}
}
}
我进入调试窗口:
deleteRecordsOneAtATime()
Number of records in CloudKit= 97
records.count= 97
recordIDsToDelete:
[<CKRecordID: 0x283622ec0; recordName=600B7BFE-04FE-4F63-BC4C-5AD1AE08908D, zoneID=...>, ... ]
localRecordID: Optional(<CKRecordID: 0x2832b0e40; recordName=835E5ABF-0125-4D3D-9ADC-28341A0E6318, zoneID=_defaultZone:__defaultOwner__>)
localRecordID: Optional(<CKRecordID: 0x2832b93c0; recordName=83C3F0CA-957A-4590-84BE-C9BB86CB3EA6, zoneID=_defaultZone:__defaultOwner__>)
localRecordID: Optional(<CKRecordID: 0x2832b0ee0; recordName=83115D59-AAEC-4B16-B81E-EEB255D66F2A, zoneID=_defaultZone:__defaultOwner__>)
...
尝试这些更改;
之后
let operation = CKModifyRecordsOperation(recordsToSave: nil, recordIDsToDelete: recordIDsToDelete)
添加
operation.database = privateDatabase
operation.queuePriority = .veryHigh
operation.configuration = CKOperation.Configuration()
operation.configuration.qualityOfService = .userInteractive
然后开始操作;
operation.start()
而不是;
privateDatabase.add(operation)