Can the recordsToSave property of CKModifyRecordsOperation object be safely used in its modifyRecordsCompletionBlock

367 Views Asked by At

Suppose I start a saveOperation using a CKModifyRecordsOperation object. Can I safely assume that the recordsToSave of the object will store the list of records given at start when I access it within the modifyRecordsCompletionBlock that is executed after the operation completes.

I would assume so, but then I saw this line in the Apple doc (basically not sure what they mean by "initial": The initial contents of the array are set to the records you specified in the initWithRecordsToSave:recordIDsToDelete: method. You can modify this array as needed before executing the operation.

If there are rare circumstances where it can change, then I want to go another way in my retry logic.

EDIT added code

   CKModifyRecordsOperation *saveOperation = [[CKModifyRecordsOperation alloc] initWithRecordsToSave:recordsToSave recordIDsToDelete:nil] ;
saveOperation.modifyRecordsCompletionBlock = completionBlock ; //see completion block definition below
[self.publicDatabase addOperation:saveOperation] ; 

[self.OperationQ addObject: saveOperation] ; //Saved in Q for later retrieval 

completionBlock is defined as

  ^(NSArray *savedRecords, NSArray *deletedRecordIDs, NSError * operationError){
 if(operationError)
 {
    DDLogError(@"Save of Touch event records failed with error %@",operationError) ;
    //Retry, can I do this and safely assume first record retrieved here is the first record I inserted into original recordsToSave array

    CKRecord *cardinalRecord = self.OperationQ[0].recordsToSave[0] ;

     //Read a field from it to decide how to handle retry (e.g: retry after delay if important set of records, don't retry if not etc)..
 }
 else
 {
   //Handle success case
 }
}
2

There are 2 best solutions below

4
rmaddy On BEST ANSWER

Based on the code you added to the question, it seems that you wish to retrieve the array of records originally passed to the modification operation.

Accessing self.OperationQ[0].recordsToSave will certainly give you back the same array passed into [[CKModifyRecordsOperation alloc] initWithRecordsToSave:recordsToSave recordIDsToDelete:nil]

The message you reference from Apple's docs simply means that if your code updated the contents of recordsToSave, it is safe to make those changes up until you call addOperation:.

The operation won't ever change that array. So if you don't change it, then accessing it in the completion block will give you back exactly what you passed in originally.

2
user3069232 On

In short No. The list of records you get at the end will be the ones that CloudKit has successfully updated. There is a possibility that it failed to update one or more in which case you need to take appropriate action.

Take a closer look at this apple documentation page https://developer.apple.com/library/ios/documentation/CloudKit/Reference/CloudKit_constants/index.html#//apple_ref/doc/constant_group/Record_Changed_Error_Keys

Which details the sort of scenarios that you need to think about.