Search code examples
iosxcodecloudkit

CloudKit Public Records And Changes not Downloaded


CloudKit Public Records And Changes not Downloaded

I have a CloudKit app with records for both a Public and a Custom Private Zone. I seem to have the change token process working for the custom private zone but am unable to get the public data to work. The code I am using is identical for both databases except for the public/private names and using the default zone for the public. I understand that subscriptions do not work on default zones, but I could not find any references to limitations on change tokens for public data. Xcode 10.1, iOS 12.0

I create my PubicData class and initialize it:

    var publicDatabase : CKDatabase!

init() {
    let kAppDelegate = UIApplication.shared.delegate as! AppDelegate
    context = kAppDelegate.context
    let container = CKContainer.default()
    publicDatabase = container.publicCloudDatabase
}//init

the download function that is called from the app entry scene - a tableview:

func downloadPublicUpdates(finishClosure : @ escaping(UIBackgroundFetchResult) -> Void) {

    var listRecordsUpdated : [CKRecord] = []
    var listRecordsDeleted : [String : String] = [:]

    var publicChangeToken : CKServerChangeToken!
    var publicChangeZoneToken : CKServerChangeToken!

    let userSettings = UserDefaults.standard

    if let data = userSettings.value(forKey: "publicChangeToken") as? Data {
        if let token = try? NSKeyedUnarchiver.unarchivedObject(ofClass : CKServerChangeToken.self, from : data) {
            publicChangeToken = token
            print("publicChangeToken exists")
        }
    } else {
        print("userSettings entry for publicChangeToken does not exist")
    }//if let data

    if let data = userSettings.value(forKey: "publicChangeZoneToken") as? Data {

        if let token = try? NSKeyedUnarchiver.unarchivedObject(ofClass: CKServerChangeToken.self, from: data) {
            publicChangeZoneToken = token
        }

    }//if let data

    let zone = CKRecordZone.default()
    var zonesIDs : [CKRecordZone.ID] = [zone.zoneID]

    let operation = CKFetchDatabaseChangesOperation(previousServerChangeToken: publicChangeToken)

    operation.recordZoneWithIDChangedBlock = {(zoneID) in
        zonesIDs.append(zoneID)
    }

    operation.changeTokenUpdatedBlock = {(token) in
        publicChangeToken = token
    }

    operation.fetchDatabaseChangesCompletionBlock = {(token, more, error) in
        if error != nil{
            finishClosure(UIBackgroundFetchResult.failed)
        } else if !zonesIDs.isEmpty {
            publicChangeToken = token

            let configuration = CKFetchRecordZoneChangesOperation.ZoneConfiguration()
            configuration.previousServerChangeToken = publicChangeZoneToken

            let fetchOperation = CKFetchRecordZoneChangesOperation(recordZoneIDs: zonesIDs, configurationsByRecordZoneID: [zonesIDs[0] : configuration])

            fetchOperation.recordChangedBlock = {(record) in
                listRecordsUpdated.append(record)
            }//fetchOperation.recordChangedBlock

            fetchOperation.recordWithIDWasDeletedBlock = {(recordID, recordType) in
                listRecordsDeleted[recordID.recordName] = recordType
            }//fetchOperation.recordWithIDWasDeletedBlock

            fetchOperation.recordZoneChangeTokensUpdatedBlock = {(zoneID, token, data) in
                publicChangeZoneToken = token
            }//fetchOperation.recordZoneChangeTokensUpdatedBlock

            fetchOperation.recordZoneFetchCompletionBlock = {(zoneID, token, data, more, error) in

                if let ckerror = error as? CKError {
                    self.processErrors(error: ckerror)

                } else {
                    publicChangeZoneToken = token

                    self.updateLocalRecords(listRecordsUpdated : listRecordsUpdated)
                    self.deleteLocalRecords(listRecordsDeleted : listRecordsDeleted)
                    listRecordsUpdated.removeAll()
                    listRecordsDeleted.removeAll()

                }//if else
            }//fetchOperation.recordZoneFetchCompletionBlock

            fetchOperation.fetchRecordZoneChangesCompletionBlock = {(error) in
                if error != nil {
                    print("Error fetchRecordZoneChangesCompletionBlock")
                    finishClosure(UIBackgroundFetchResult.failed)
                } else {

                    if publicChangeToken != nil {
                        if let data = try? NSKeyedArchiver.archivedData(withRootObject: publicChangeToken, requiringSecureCoding: false) {
                            userSettings.set(data, forKey : "publicChangeToken")
                        }
                    }//if changeToken != nil

                    if publicChangeZoneToken != nil {
                        if let data = try? NSKeyedArchiver.archivedData(withRootObject: publicChangeZoneToken, requiringSecureCoding: false) {
                            userSettings.set(data, forKey : "publicChangeZoneToken")
                        }
                    }
                    //self.updateInterface()
                    self.updateLocalReferences()
                    finishClosure(UIBackgroundFetchResult.newData)
                }
            }//fetchOperation.fetchRecordZoneChangesCompletionBlock

            self.publicDatabase.add(fetchOperation)

        } else {//else if !zonesIDs.isEmpty
            finishClosure(UIBackgroundFetchResult.noData)
        }//if zoneid not empty

    }//fetchDatabaseChangesCompletionBlock

    print("listRecordsUpdated.count is \(listRecordsUpdated.count)")

    publicDatabase.add(operation)

}//downloadPublicUpdates

Outside of class: var PD = PDData()

I call the download method in viewDidLoad from the initial TableViewController:

    PD.downloadPublicUpdates { (result) in
        print("in ctvc viewDidLoad and downloadPublicUpdates")

        switch result {
        case .noData:
            print("no data")
        case .newData:
            print("new data")
        case .failed:
            print("failed to get data")
        }//switch

    }//downloadPublicUpdates

The console output is always:

userSettings entry for publicChangeToken does not exist listRecordsUpdated.count is 0 in ctvc viewDidLoad and downloadPublicUpdates failed to get data

Any guidance would be appreciated.


Solution

  • There are no change tokens available in a public database. Those only exist in private and shared databases.

    To keep things in sync, you typically have to keep a modification date on records locally, and then query for stuff that is newer on the CloudKit server using a CKQueryOperation.

    Good luck!