Retrieving changes from CloudKit

To fetch changes that occurred after a certain point in time, you need to a change token along with your fetch operations. Add the following computed property to the CloudStore class so you can keep track of the change token that represents the last time the app fetched changes from CloudKit:

var privateDatabaseChangeToken: CKServerChangeToken? {
  get { return UserDefaults.standard.serverChangeToken(forKey: "CloudStore.privateDatabaseChangeToken") }
  set { UserDefaults.standard.set(newValue, forKey: "CloudStore.privateDatabaseChangeToken") }
}

This computed property uses a helper method on UserDefaults to store CKServerChangeToken.

If you're curious to see how the computed property is implemented, make sure ...

Get Mastering iOS 12 Programming - Third Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.