Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Deleting all records CloudKit every day from a particular record type

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?

like image 485
bhzag Avatar asked Feb 05 '15 06:02

bhzag


3 Answers

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.

like image 143
Edwin Vermeer Avatar answered Nov 04 '22 04:11

Edwin Vermeer


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)
}
like image 23
Shaybc Avatar answered Nov 04 '22 05:11

Shaybc


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.

like image 8
Pranav Wadhwa Avatar answered Nov 04 '22 05:11

Pranav Wadhwa