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 O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.