In CloudKit, after adding a record, the new record will not appear in a query that is conducted right away. How to solve it?

二次信任 提交于 2019-12-11 11:53:00

问题


I am developing an iOS app using CloudKit. I have a problem: After adding a record using CKModifyRecordsOperation, if I do a query right away using CKQueryOperation to get all the records, then usually the newest record doesn't appear in the query result. It seems that the newest record will appear in queries that are conducted several seconds after the writing operation. So, how to solve it?

In my app, when the user add a new record, then the app will refresh the list of all the records. But in most cases, the list will not have the new record. Only after the user refresh the list later, the record will appear.


回答1:


I banged my head against the same issue and talked to Apple support to get an official answer:

"The behavior you described is expected because CloudKit will take a while to index the changes and the records that are not indexed yet won’t be returned.

The solution is to use CKFetchRecordChangesOperation, if the data is in a custom zone of the private database. If you are using the public database, or the default zone of the private database, think about this:

  1. The device making the changes knows the changes. [meaning: keep a cache of the updates you make]
  2. The other peer devices observe the changes using CKQuerySubscription."



回答2:


get the number of records just before you do the update

then in the code where you do the update, at the end of the update, get another count of the records. If this count is the same as the count before the update was made, "wait" (swift has a wait function) there till the count is one more than the preupdate count



来源:https://stackoverflow.com/questions/34824301/in-cloudkit-after-adding-a-record-the-new-record-will-not-appear-in-a-query-th

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!