代码之家  ›  专栏  ›  技术社区  ›  Clifton Labrum

CloudKit错误:更改令牌已过期,需要重置

  •  1
  • Clifton Labrum  · 技术社区  · 8 年前

    Swift 3.1,Xcode 8.3.3

    我一直从CloudKit收到一个错误,我不知道该怎么办。

    let operation = CKFetchNotificationChangesOperation(previousServerChangeToken: previousChangeToken)
    
    //Hold the notification IDs we processed so we can tell CloudKit to never send them to us again
    var notificationIDs = [CKNotificationID]()
    
    operation.notificationChangedBlock = { [weak self] notification in
      guard let notification = notification as? CKQueryNotification else { return }
      if let id = notification.notificationID {
        notificationIDs.append(id)
      }
    }
    
    operation.fetchNotificationChangesCompletionBlock = { [weak self] newToken, error in
      if let error = error{
        print(error) //<-- <!> This is the error <!>
      }else{
        self?.previousChangeToken = newToken
    
        //All records are in, now save the data locally
        let fetchOperation = CKFetchRecordsOperation(recordIDs: recordIDs)
    
        fetchOperation.fetchRecordsCompletionBlock = { [weak self] records, error in
          if let e = error {
            print("fetchRecordsCompletionBlock Error fetching: \(e)")
          }
          //Save records to local persistence...
        }
    
        self?.privateDB.add(fetchOperation)
    
        //Tell CloudKit we've read the notifications
        let operationRead = CKMarkNotificationsReadOperation(notificationIDsToMarkRead: notificationIDs)
        self?.container.add(operationRead)
      }
    }
    
    container.add(operation)
    

    错误显示:

    <CKError 0x174241e90:“更改令牌已过期”(21/1016);服务器消息

    CKServerChangeToken documentation

    知道我该做什么吗?

    1 回复  |  直到 8 年前
        1
  •  2
  •   Dave DeLong    8 年前

    此错误代码为 CKErrorCodeChangeTokenExpired

    https://developer.apple.com/documentation/cloudkit/ckerror/2325216-changetokenexpired

    当更改令牌太旧或容器已重置(重置容器会使旧的更改令牌无效)时,返回此错误代码。

    (描述代码本身):

    previousServerChangeToken值太旧,客户端必须从头开始重新同步

    (在各种提取操作完成/更新块上):

    如果服务器返回CKErrorChangeTokenExpired错误,则初始化此操作时用于此记录区域的serverChangeToken太旧,客户端应丢弃其本地缓存,并从nil serverChangeToken开始重新获取此记录区域中的更改。

    推荐文章