How to update data in TableView without the delay using CloudKit when Creating new Records

There are 2 View Controllers in my App.

The first "MainViewController" displays a tableView with CKRecords fields fetched from the private CloudKit database. Inside the viewWillAppear method of this VC I fetch records from CloudKit and reload data of a tableview to show the latest fetched results that have been previously saved in the CloudKit by the user.

The second view controller "CreateRecordViewController" is made for creating CKRecords and saving them to the private database of the CloudKit.

So i create records in the CreateRecordViewController and show them in the MainViewController.

The problem is the following: when I create record at CreateRecordViewController, it is saving on the CloudKit server, but after closing this CreateRecordViewController and going to MainViewController the tableview does not always update in time.

Sometimes when CloudKit servers work faster i can see the new records in a tableView, but most of the time there is a delay (i don't see the new results in a tableview at the moment when MainViewController loads) I think this is because when i fetch records it fetches the old data (not sure why), but maybe i've made another mistake .
This is a bad user experience and i would like to know how to avoid this delay. I want my tableView to show the updated results just after i close CreateRecordViewController .

My first idea was to subscribe to CloudKit Records changes, and fetch and reload data in a tableView when notification received, but i really don't need a push notification (i'd rather have just a method in code where i could fetch data from CloudKit after i know that all CloudKit records saved or after i know that there is a new record created, and after fetching and getting the data for a tableView i would call tableView.reloadData for example), but i'm not sure how to implement this right (in what method) and not sure if this is the best solution. I've also heard that in WWDC 2016 video dedicated to CloudKit, that now there are some new methods related to subscription to Record changes, maybe some of those methods can help (not sure). Looking for the best, or any good and easy solution for this problem (Delay Problem).

There is no guarantee as to when the record would be available in a query but there is something you can do. You can stitch the new record back in. Because when you create and save a record you have the record id you can make a ckfetchrecordsoperation and pass the id from the new record and you are guaranteed to get it back immediately. The indexing sometimes can take a while and this is frustrating with CloudKit. So basically the best way to guarantee a speedy database is make a query and if the new record id is not in there make a fetch with the id and append it to your results. Hope this makes sense.

I think the part you are not getting is the record may or may not come down with the query in viewcontroller 1 because of the way the indexing works. You even mention in your question it fetches old data. This is due to the server indexing. The same would happen if you deleted a record. It could still show up for some time in the query. In that case you keep track of the recently deleted record ids and remove them after the query. Again this manually adding and removing I am talking about is the only way to guarantee what the users see and the results from the query stay in sync with what the user would expect.

Here is some code although completely untested that I hope will help you visualize what I am saying above.

It's a bit of a mess but you can see that I am stitching the missing recordID if not nil back into the query that you are doing because that query is not guaranteed in real time to give you your expected new records. In this case self.passedBackNewRecordID is set based on the new recordID from Viewcontroller 2. How you set this or track this variable is up to you but you probably need an entire queue system because what I am telling you applies for changes to the record as well as deletes. So in a production app I had to track the records that had changes, deletes and additions and get the fresh version of each of those so you can imagine the complexity of a list of objects. Since I stopped using CloudKit because the tombstoning or indexing takes too long to show changes in queries.