I am working on an update to one of my apps. The app uses CloudKit and utilizes the Public and Private databases. The user data is stored in the Private database. I have created methods to convert from CKRecords of the user's type to Codeable Structs and encode in json format and decode the json data into the array of Structs. I also have a method to create a CKRecord of the user's type from the Struct data. I am working on giving the user the option of replacing the CKRecords in the cloud with CKRecords created from a json file. I have a method which deletes all of the CKRecords followed by a method which uploads the CKRecords created from the json data. If I wait a while after this operation without using the app, it works well. But if I try to access the saved CKRecords shortly after the methods are executed, I have mixed results. Sometimes, I get less than what I uploaded and sometimes I get more than what I uploaded. I use try await on both methods and use the return value of the first method (delete) as an input into the second method (upload). Both the delete and upload methods use
modifyRecords(saving: saveRecords, deleting: recordIDs, savePolicy: .allKeys, atomically: false)
I do not use the modifyRecords method to do both at the same time. I use two modifyRecords methods.
In all cases, the results show that the operations completed successfully even when there was an issue.
I'd like for the delete method to delete all of the data and for the upload method to upload all of the json data without requiring an excessive delay.. Any advice would be most appreciated.