阅读背景:

每天从特定记录类型中删除所有记录CloudKit

来源:互联网 

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?

所以我想每天擦除特定记录类型的每条记录。所以基本上,我希望在凌晨12点擦除数据,以便第二天新鲜。我该怎么做呢?这是我可以在CloudKit仪表板中设置的内容还是我必须以编程方式设置它?

4 个解决方案

#1


9  

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

最好的解决方法是创建一个单独的recordType,每天包含一条记录。然后在当天要删除的记录中设置CKReference到该特定日记录并将其操作设置为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.

之后,您只需删除日记录,所有相关记录将被删除。可以从仪表板轻松删除该记录,也可以在应用程序中创建功能,也可以为管理操作创建第二个应用程序。

#2


6  

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.

“RECORD TYPE”应该是您的记录类型。希望这可以帮助。

#3


4  

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)
}

#4


1  

this code able to delete any amount of records

此代码能够删除任意数量的记录

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)
    }
 }

分享到: