所以我想每天擦除特定记录类型的每条记录。所以基本上,我希望在凌晨12点擦除数据,以便第二天新鲜。我该怎么做呢?这是我可以在CloudKit仪表板中设置的,还是我必须以编程方式设置它?
答案 0 :(得分:10)
如果您需要删除多条记录,则从仪表板中删除记录非常有用。
最佳解决方法是创建一个单独的recordType,每天包含一条记录。然后在当天要删除的记录中设置CKReference到该特定日记录并将其操作设置为CKReferenceAction.DeleteSelf
之后,您只需删除日记录,所有相关记录将被删除。可以从仪表板轻松删除一条记录,也可以在应用程序中创建功能,也可以为管理操作创建第二个应用程序。
答案 1 :(得分:7)
func deleteAllRecords()
{
let publicDatabase: CKDatabase = CKContainer.defaultContainer().publicCloudDatabase
// fetch records from iCloud, get their recordID and then delete them
var recordIDsArray: [CKRecordID] = []
let operation = CKModifyRecordsOperation(recordsToSave: nil, recordIDsToDelete: recordIDsArray)
operation.modifyRecordsCompletionBlock = {
(savedRecords: [CKRecord]?, deletedRecordIDs: [CKRecordID]?, error: NSError?) in
print("deleted all records")
}
publicDatabase.addOperation(operation)
}
答案 2 :(得分:7)
尝试这样的事情:
let publicDb = CKContainer.defaultContainer().publicCloudDatabase
let query = CKQuery(recordType: "RECORD TYPE", predicate: NSPredicate(format: "TRUEPREDICATE", argumentArray: nil))
publicDb.performQuery(query, inZoneWithID: nil) { (records, error) in
if error == nil {
for record in records! {
publicDb.deleteRecordWithID(record.recordID, completionHandler: { (recordId, error) in
if error == nil {
//Record deleted
}
})
}
}
}
"记录类型"应该是你的记录类型。希望这会有所帮助。
答案 3 :(得分:1)
此代码能够删除任意数量的记录
import CloudKit
class iCloudDelete {
private let cloudDB: CKDatabase
private var recordIDsToDelete = [CKRecordID]()
private var onAllQueriesCompleted : (()->())?
public var resultsLimit = 10 // default is 100
init(cloudDB: CKDatabase){
self.cloudDB = cloudDB
}
func delete(query: CKQuery, onComplete: @escaping ()->Void) {
onAllQueriesCompleted = onComplete
add(queryOperation: CKQueryOperation(query: query))
}
private func add(queryOperation: CKQueryOperation) {
queryOperation.resultsLimit = resultsLimit
queryOperation.queryCompletionBlock = queryDeleteCompletionBlock
queryOperation.recordFetchedBlock = recordFetched
cloudDB.add(queryOperation)
}
private func queryDeleteCompletionBlock(cursor: CKQueryCursor?, error: Error?) {
print("-----------------------")
delete(ids: recordIDsToDelete) {
self.recordIDsToDelete.removeAll()
if let cursor = cursor {
self.add(queryOperation: CKQueryOperation(cursor: cursor))
} else {
self.onAllQueriesCompleted?()
}
}
}
private func recordFetched(record: CKRecord) {
print("RECORD fetched: \(record.recordID.recordName)")
recordIDsToDelete.append(record.recordID)
}
private func delete(ids: [CKRecordID], onComplete: @escaping ()->Void) {
let delete = CKModifyRecordsOperation(recordsToSave: nil, recordIDsToDelete: ids)
delete.completionBlock = {
onComplete()
}
cloudDB.add(delete)
}
}