首页 > 解决方案 > CloudKit,重新安装应用程序后,如何将我的订阅重置为当前记录状态?

问题描述

我正在处理这样一种情况,即用户之前删除了该应用程序,现在又重新安装了它。

它击中了我的 delta fetch 功能,该功能正在接收很多旧的订阅通知,主要是删除。但不下载当前记录。

我现在添加代码以对每种记录类型执行提取以下载所有数据。

我想重置 delta fetch 服务器令牌,因此该应用程序不必处理旧订阅通知。但是我找不到如何做到这一点,也许这是不可能的。

标签: ioscocoa-touchcloudkitcksubscription

解决方案


当您说“delta fetch server token”时,您指的是CKServerChangeToken文档)吗?您是否尝试在 CloudKit私有数据库中进行同步?

假设这是真的,下面是我如何从私有数据库中获取更改并跟踪同步令牌的示例:

//MARK: Fetch Latest from CloudKit from private DB
func fetchPrivateCloudKitChanges(){
  print("Fetching private changes...")

  //:::
  let privateZoneId = CKRecordZone.ID(zoneName: CloudKit.zoneName, ownerName: CKCurrentUserDefaultName)

  /----
  let options = CKFetchRecordZoneChangesOperation.ZoneOptions()
  options.previousServerChangeToken = previousChangeToken

  let operation = CKFetchRecordZoneChangesOperation(recordZoneIDs: [privateZoneId], optionsByRecordZoneID: [recordZoneID:options])

  //Queue up the updated records to process below
  var records = [CKRecord]()

  operation.recordChangedBlock = { record in
    records.append(record)
  }

  operation.recordWithIDWasDeletedBlock = { recordId, type in
    //Process a deleted record in your local database...
  }

  operation.recordZoneChangeTokensUpdatedBlock = { (zoneId, token, data) in
    // Save new zone change token to disk
    previousChangeToken = token
  }

  operation.recordZoneFetchCompletionBlock = { zoneId, token, _, _, error in
    if let error = error {
      print(error)
    }
    // Write this new zone change token to disk
    previousChangeToken = token
  }

  operation.fetchRecordZoneChangesCompletionBlock = { error in
    if let error = error {
      print(error
    }else{
      //Success! Process all downloaded records from `records` array above...
      //records...
    }
  }

  CloudKit.privateDB.add(operation)
}

//Change token property that gets saved and retrieved from UserDefaults
var previousChangeToken: CKServerChangeToken? {
  get {
    guard let tokenData = defaults.object(forKey: "previousChangeToken") as? Data else { return nil }
    return NSKeyedUnarchiver.unarchiveObject(with: tokenData) as? CKServerChangeToken
  }
  set {
    guard let newValue = newValue else {
      defaults.removeObject(forKey: "previousChangeToken")
      return
    }

    let data = NSKeyedArchiver.archivedData(withRootObject: newValue)
    defaults.set(data, forKey: "previousChangeToken")
  }
}

您的具体情况可能会有所不同,但我认为在与 CloudKit 保持同步时,它通常应该是这样工作的。

更新

您可以尝试将记录存储previousServerChangeTokenUsersCloudKit 中(您必须将其添加为新字段)。每次您所做的previousServerChangeToken更改recordZoneFetchCompletionBlock都必须将其保存回 iCloud 中的用户记录中。


推荐阅读