1

我是 CloudKit 的新手,并且已经弄清楚了一次CKModifyRecordsOperation添加/删除/删除一个的基本案例CKRecord。我想用它来传递多个recordsToSaveand recordIDsToDeleteerror.userInfo[CKErrorRetryAfterKey]我知道当有值时我需要重试。我的问题是,如何重试此操作?我一直在弄清楚如何知道要重试哪些记录。我必须重试所有记录吗?我可以在哪个完成块中重试,还是两者兼而有之?

我的代码在下面带有我不明白如何重试的注释。

func modifyCkRecs(recsToAddOrMod: [CKRecord]?, recordIDsToDelete:[CKRecordID]?, numTries: Int) {


let ops = CKModifyRecordsOperation(recordsToSave: recsToAddOrMod, recordIDsToDelete: recordIDsToDelete)
ops.savePolicy = CKRecordSavePolicy.AllKeys

ops.perRecordCompletionBlock =  { record, error in

  if let error = error {

    var message = error.localizedDescription
    if error.code == CKErrorCode.NotAuthenticated.rawValue {
      print("Not authenticated")
      message = "If you want your data the same on all your Apple devices, then log into iCloud on your device and make sure the iCloud drive is turned on for this app.\nTo enable iCloud, go to Settings -> iCloud"
    }
    if error.code == CKErrorCode.ServiceUnavailable.rawValue {
      print("Service unavailable")
    }
    if error.code == CKErrorCode.RequestRateLimited.rawValue {
      print("Request Rate Limited")
    }
    if error.code == CKErrorCode.NetworkFailure.rawValue {
      print("Network failure!!")
    }
    if error.code == CKErrorCode.InvalidArguments.rawValue {
      print("Invalid Arguments!!")
    }
    print("Error msg: \(message)")

//        if let delayTime = self.retryTime(error) {
//          if numTries < CloudKitAPI.MAX_RETRY {
//            
//            dispatch_after(delayTime, dispatch_get_global_queue(QOS_CLASS_BACKGROUND, 0)) {
//              self.modifyCkRecs(????,????, numTries: numTries+1)
//            }
//          }
//          else {
//            print("WARNING: Reached MAX number of retry, giving up")
//          }
//        }
  }
  else {
    print("Update was a success!")
    if let updatedRec = record {
      if let coreDataObj = self.convertRecToCoreData(updatedRec) {
        // archive the CKRecord system fields into Core Data
        coreDataObj.archiveCkMetadata(updatedRec)

        // remove from queue
        self.addedOrModQueue.removeValueForKey(coreDataObj.uuid)
        self.saveAMQueue()
      }
    }
  }
}

// TODO: Figure out what this block is good for
ops.modifyRecordsCompletionBlock = { savedRecords, deletedRecordIDs, error in
  if let error = error {

    var message = error.localizedDescription
    if error.code == CKErrorCode.NotAuthenticated.rawValue {
      print("Not authenticated")
      message = "If you want your data the same on all your Apple devices, then log into iCloud on your device and make sure the iCloud drive is turned on for this app.\nTo enable iCloud, go to Settings -> iCloud"
    }
    if error.code == CKErrorCode.ServiceUnavailable.rawValue {
      print("Service unavailable")
    }
    if error.code == CKErrorCode.RequestRateLimited.rawValue {
      print("Request Rate Limited")
    }
    if error.code == CKErrorCode.NetworkFailure.rawValue {
      print("Network failure!!")
    }
    if error.code == CKErrorCode.InvalidArguments.rawValue {
      print("Invalid Arguments!!")
    }
    print("Error msg: \(message)")

    // RETRY LOGIC HERE????
    // SOMETHING LIKE THE ABOVE BUT WITH THESE ARGS:
    // self.modifyCkRecs(recsToAddOrMod, recordIDsToDelete, numTries: numTries+1)
  }
  else {

    // clean up the deleted queue
    if let deletedRecs = deletedRecordIDs {
      for deletedRecId in deletedRecs {
        if let uuid = NSUUID(UUIDString: deletedRecId.recordName), let index = self.deletedQueue.indexOf(uuid) {
          self.deletedQueue.removeAtIndex(index)
        }
      }
      self.saveDQueue()
    }
  }
}

ops.qualityOfService = .Background
privateDB.addOperation(ops)
}
4

0 回答 0