Search code examples
objective-ccloudkitckrecord

Reading CloudKit records after saving it


I create a CloudKit record using - (void)saveRecord:(CKRecord *)record completionHandler:(void (^)(CKRecord *record, NSError *error))completionHandler; function.

Then I fetch all records of this type using - (void)performQuery:(CKQuery *)query inZoneWithID:(CKRecordZoneID *)zoneID completionHandler:(void (^)(NSArray /* CKRecord */ *results, NSError *error))completionHandler; in the completionHandler of saveRecord. But in the result of this query there is no record that I added just before.

Then when I perform query again after some time I have all records including the one I just added. How I can now that the record that I've just added is already in the database. Shouldn't it be available already in completion handler?


Solution

  • There is no reason to query for the saved record in the completion handler. The completion handler of saveRecord:completionHandler: gives you the saved record if it succeeded or an error if it doesn't.

    BTW - the reason the query fails is because you are doing it too soon. It takes a little time for the saved record to be uploaded and indexed. This is why you need to use the parameters of the completion handler instead of performing the query so soon.