1

I am using the code below to update a record. Once the record has been updated I would like to run my refresh function. At the moment the refresh function is sometimes called before the record has been updated so the refresh results are the same as before the record was updated.

Thanks

var tempDocumentsArray:NSArray!
let recordID = CKRecordID(recordName: "layerAbove")
var predicate = NSPredicate(format: "recordID = %@", recordID)
let query = CKQuery(recordType: "Layers", predicate: predicate)
self.publicDB.performQuery(query, inZoneWithID: nil) { (results, error) -> Void in
tempDocumentsArray = results
print("Results are: \(tempDocumentsArray)")
let record = tempDocumentsArray[0] as! CKRecord

var layerAbovePrevPos = record.objectForKey("layerNumber") as! Int
layerAbovePrevPos = layerAbovePrevPos - 1
let nlnChanged = record.setObject(layerAbovePrevPos, forKey: "layerNumber")

self.publicDB.saveRecord(record, completionHandler: { (returnRecord, error) -> Void in
    if let err = error {
        print("Error: \(err.localizedDescription)")
    } else {
        dispatch_async(dispatch_get_main_queue()) {
            print("Success")


            //TODO:This is sometimes called before the save is complete!
            self.resetAndGet()


        }
    }
})
}
Tom Coomer
  • 6,227
  • 12
  • 45
  • 82
  • After the save, do you read the record again? Couldn't you use the updated record (returnRecord) instead? That would save you a CloudKit fetch operation. Are you sure the bug is not in your resetAndGet method? – Edwin Vermeer Aug 31 '15 at 05:32
  • In some cases there are multiple records that need to be fetched with the resetAndGet. Also the method works correctly if it is called by tapping the button a few seconds after the record was saved. – Tom Coomer Aug 31 '15 at 10:05

0 Answers0