CKModifyRecordsOperation错误重试逻辑

时间:2016-11-04 05:42:05

标签: swift cloudkit nsoperation ckerror

我是CloudKit的新手,并且已经找出CKModifyRecordsOperation一次添加/删除/删除一个CKRecord的基本情况。我想用它来传递多个recordsToSaverecordIDsToDelete。我知道当error.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)
}

0 个答案:

没有答案