将记录导出到cloudkit

时间:2015-07-18 02:11:27

标签: cloudkit

我需要将数据从12,000个项目的字典导出到Cloudkit。我尝试使用便捷API,但在尝试保存到公共数据库时,我一直在达到速率限制。我然后尝试了Operation API,我得到了类似的错误。我的问题是:如何在不超出限制的情况下将大量数据保存到cloudkit中?

3 个答案:

答案 0 :(得分:1)

根据CKErrorLimitExceeded错误代码的文档,您应该

  

尝试将您的请求重构为多个较小的批次。

因此,如果CKModifyRecordsOperation操作导致CKErrorLimitExceeded错误,您只需创建两个CKModifyRecordsOperation对象,每个对象都有一半来自失败操作的数据。如果你以递归方式执行此操作(因此任何拆分操作也可能因超出限制错误而失败,再次拆分为两个),那么最终应该获得一些CKModifyRecordsOperation个对象,这些对象具有足够少的记录以避免错误。

答案 1 :(得分:0)

如果您有自己的服务器,可以尝试CloudKit Web Service API。

答案 2 :(得分:0)

在iOS 10中,每次操作允许的最大记录数为400.

/// The system allowed maximum record modifications count.
///
/// If excute a CKModifyRecordsOperation with more than 400 record modifications, system will return a CKErrorLimitExceeded error.
private let maximumRecordModificationsLimit = 400

private func modifyRecords(recordsToSave: [CKRecord], recordIDsToDelete: [CKRecordID], previousRetryAfterSeconds: TimeInterval = 0, completion: ((Bool) -> Void)? = nil) {

    guard !recordsToSave.isEmpty || !recordIDsToDelete.isEmpty else {
        completion?(true)
        return
    }

    func handleLimitExceeded() {
        let recordsToSaveFirstSplit = recordsToSave[0 ..< recordsToSave.count / 2]
        let recordsToSaveSecondSplit = recordsToSave[recordsToSave.count / 2 ..< recordsToSave.count]
        let recordIDsToDeleteFirstSplit = recordIDsToDelete[0 ..< recordIDsToDelete.count / 2]
        let recordIDsToDeleteSecondSplit = recordIDsToDelete[recordIDsToDelete.count / 2 ..< recordIDsToDelete.count]

        self.modifyRecords(recordsToSave: Array(recordsToSaveFirstSplit), recordIDsToDelete: Array(recordIDsToDeleteFirstSplit))
        self.modifyRecords(recordsToSave: Array(recordsToSaveSecondSplit), recordIDsToDelete: Array(recordIDsToDeleteSecondSplit), completion: completion)
    }

    if recordsToSave.count + recordIDsToDelete.count > maximumRecordModificationsLimit {
        handleLimitExceeded()
        return
    }

    // run CKModifyRecordsOperation at here
}