未下载CloudKit公共记录和更改

时间:2018-11-16 20:08:37

标签: ios xcode cloudkit

未下载CloudKit公开记录和更改

我有一个CloudKit应用,其中包含公共和自定义私有记录 区。我似乎已经将变更令牌流程用于自定义 私有区域,但无法获取公共数据。我的代码 除公共/私有数据库外,两个数据库的使用相同 名称和使用默认区域为公众。我明白那个 订阅不适用于默认区域,但是我找不到任何 引用公共数据更改令牌的限制。 Xcode 10.1,iOS 12.0

我创建我的PubicData类并对其进行初始化:

    var publicDatabase : CKDatabase!

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

从应用程序入口场景调用的下载功能-表格视图:

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

课程外:var PD = PDData()

我从最初的TableViewController调用viewDidLoad中的下载方法:

    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

控制台输出始终为:

publicChangeToken的userSettings条目不存在 listRecordsUpdated.count为0 在ctvc中查看加载并下载PublicUpdates 无法获取数据

任何指导将不胜感激。

1 个答案:

答案 0 :(得分:3)

公共数据库中没有可用的更改令牌。这些仅存在于私有和共享数据库中。

要保持同步,通常必须在本地记录上保留修改日期,然后使用CKQueryOperation在CloudKit服务器上查询更新的内容。

祝你好运!