So I currently use the following methods to get and set zone change tokens:
private func zoneChangeToken(for zoneID: CKRecordZoneID) -> CKServerChangeToken? {
let cacheKey = self.name(for: self.database) + "." + zoneID.zoneName + "." + zoneID.ownerName
if let data = UserDefaults.standard.data(forKey: cacheKey) {
return NSKeyedUnarchiver.unarchiveObject(with: data) as? CKServerChangeToken
} else {
return nil
}
}
private func setZoneChangeToken(changeToken: CKServerChangeToken?, for zoneID: CKRecordZoneID) {
let cacheKey = self.name(for: self.database) + "." + zoneID.zoneName + "." + zoneID.ownerName
if changeToken == nil {
UserDefaults.standard.removeObject(forKey: cacheKey)
} else {
let data = NSKeyedArchiver.archivedData(withRootObject: changeToken!)
UserDefaults.standard.set(data, forKey: cacheKey)
}
}
I try to uniquely target the zone by creating a path from database to zone. However since the shared database might have multiple zones with the same zone name, I appended the zone owner name.
The question is, is this a proper way to store the zone change tokens or is there a better, more efficient, way?