1

斯威夫特 3.1,Xcode 8.3.3

我不断收到来自 CloudKit 的错误,我不知道该怎么办。

我正在跟踪来自 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);服务器消息=“错误代码:RESET_NEEDED”;uuid = ...; 容器 ID = "...">

CKServerChangeToken 文档没有提到任何关于重置令牌的内容,CloudKit 仪表板也没有提供任何此类选项。

知道我应该做什么吗?

4

1 回答 1

2

此错误代码为CKErrorCodeChangeTokenExpired,表示您需要重新同步更改。

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

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

与此错误代码相关的注释包括:

(描述代码本身):

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

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

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

于 2017-09-09T14:11:09.803 回答