CloudKit: CKFetchRecordChangesOperation, CKServerChangeToken and Delta Download

安稳与你 提交于 2019-12-03 16:36:32

I found a time to write an answer for this question. I won't dig into implementation, but I will discuss the concept.

CloudKit provides a way to data synchronisation between your device and the CloudKit server. What I use to establish synchronisation process in my case between iPhone and server only (again, if you have iPhone + iPad app, the process require more steps.):

I have custom zone in the private cloud database. I use OperationQueue to establish different asynchronous processes which depend on each other. Some operations have own operation queues.

Steps:

1) Check if my custom zone is exist

1.1) If there is no custom zone

1.2) Create new custom zone. (Optional: add records)

1.3) Refresh zone change token

You can refresh zone change token by: performing CKFetchRecordChangesOperation, fetchRecordChangesCompletionBlock returns CKServerChangeToken save it to UserDefaults (for example) using NSKeyedArchiver). This operation's task is to refresh token and it's performed at the end synchronisation process.

2) If there is custom zone already

2.1) Get changes from zone using previously saved zone change token. (CKFetchRecordChangesOperation)

2.2) Update and delete local records.

2.3) Refresh zone change token.

2.4) Check for local changes (I'm using last cloud sync timestamp to check what records was modified after).

2.5) Upload records to cloud kit database

2.6) Refresh zone change token again.

I highly recommend Nick Harris article series: https://nickharris.wordpress.com/2016/02/09/cloudkit-core-data-nsoperations-introduction/

You'll find there implementation and design concepts. It worth reading. I hope somebody'll find all of this helpful.

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