jazzedge
11/29/2017 - 9:01 PM

Swift - CloudKit - Add Save Delete Multiple Records

A group of record operations may be performed in a single transaction using the CKModifyRecordsOperation class.

See: http://www.techotopia.com/index.php/An_Introduction_to_CloudKit_Data_Storage_on_iOS_8

A group of record operations may be performed in a single transaction using the 
CKModifyRecordsOperation class. This class also allows timeout durations to be specified for 
the transaction, together with completion handlers to be called at various stages during the 
process. The following code, for example, uses the CKModifyRecordsOperation class to add three 
new records and delete two existing records in a single operation. The code also establishes 
timeout parameters and implements all three completion handlers. Once the modify operation 
object has been created and configured, it is added to the database for execution:

let modifyRecordsOperation = CKModifyRecordsOperation(
		recordsToSave: [myRecord1, myRecord2, myRecord3],
 		recordIDsToDelete: [myRecord4, myRecord5])

modifyRecordsOperation.timeoutIntervalForRequest = 10
modifyRecordsOperation.timeoutIntervalForResource = 10

modifyRecordsOperation.perRecordCompletionBlock = { record, error in
     // Called after each individual record operation completes
}

modifyRecordsOperation.perRecordProgressBlock = { record, progress in
     // Called to update the status of an indivudual operation
     // progress is a Double value indicating progress so far
}

modifyRecordsOperation.modifyRecordsCompletionBlock = { 
		records, recordIDs, error in
    // Called after all of the record operations are complete
}

privateDatabase?.add(modifyRecordsOperation)