What is a robust approach to deleting a CKRecord associated with an IndexPath in a table view or collection view?

When synchronizing model objects, local CKRecords, and CKRecords in CloudKit during swipe-to-delete, how can I make this as robust as possible? Error handling omitted for the sake of the example.

override func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCell.EditingStyle, forRowAt indexPath: IndexPath) {

        if editingStyle == .delete {

            let record = self.records[indexPath.row]

            privateDatabase.delete(withRecordID: record.recordID) { recordID, error in

                self.records.remove(at: indexPath.row)

            }

        }

    }

Since indexPath could change due to other changes in the table view / collection view during the time it takes to delete the record from CloudKit, how could this be improved upon?

CKRecord / CloudKit is a way to store your data in iCloud. It is not supposed to be used as you main data model locally. Instead you should have a local database or CoreData that contains a local copy of your data. You would then use the CKRecord to update CloudKit and make the same change in your local database. That local database would then for example feed into a diffable data source and this will trigger the updates in table view for you.

You can then modify the data source in response to the swipe (and possibly your local database) while having the delete request send to CloudKit. Basically your database is already showing the new truth. To make this really robust against other updates, it helps to keep a list of these 'in flight' entries around so that you don't add these to the data source again. You are basically creating a list of shadow updates.

What is a robust approach to deleting a CKRecord associated with an IndexPath in a table view or collection view?
 
 
Q