So I want to wipe every record for a particular record type every day. So basically, I want the data to be wiped at 12:00 AM so that it will be fresh for the next day. How would I go about doing this? Is this something that I could set up in the CloudKit dashboard or will I have to set this up programmatically?
Deleting records from the dashboard is a lot of work if you need to delete multiple records.
The best workaround is by creating a separate recordType that will contain one record for every day. Then in the records that you want deleted for that day set up a CKReference to that particular day record and set its action to CKReferenceAction.DeleteSelf
After that you only have to remove the day record and all related records will be removed. Removing that one record could easily be done from the dashboard or you could create functionality in your app or you could create a 2nd app for administrative actions.
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.add(operation)
}
Try something like this:
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
}
})
}
}
}
"RECORD TYPE" should be your record type. Hope this helps.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With