【问题标题】:Save Multiple CKRecords At One Time Using CloudKit and Swift使用 CloudKit 和 Swift 一次保存多个 CKRecord
【发布时间】:2016-06-01 18:10:18
【问题描述】:

是否可以使用 Swift 在一个 CloudKit 操作中保存多个 CKRecord 对象?我知道如何一次检索多条记录,使用CKFetchRecordsOperation 之类的东西,甚至只是普通的performQuery。我刚刚意识到我不确定在一次操作中保存多个新记录的方法。到目前为止,我一直这样做的方式对我来说似乎效率低下。例如:

let recordsToSave:[CKRecord]!

for newRecord in recordsToSave {

    if let database = self.publicDatabase {

        database.saveRecord(newRecord, completionHandler: { (record:CKRecord?, error:NSError?) in

            // Check if there was an error
            if error != nil {

                // There was an error, handle it properly.

            }
            else {

                // There wasn't an error
                if let savedRecord = record {

                    // Handle saved record accordingly.

                }

            }
        })
}

虽然这确实工作得很好而且花花公子,但在我看来,它似乎效率极低。我认为调用一个特定的函数来保存CKRecords 的整个数组会更有意义,而不是每次都通过循环调用数据库。基本上这是我希望我能做到的:

let recordsToSave:[CKRecord]!

if let database = self.publicDatabase {

    // SOMETHING HERE LIKE database.saveRECORDS (plural) ????
    database.saveRecords(recordsToSave, completionHandler: { (records:[CKRecord]?, error:NSError?) in

        // Check if there was an error
        if error != nil {

            // There was an error, handle it properly.

        }
        else {

                // There wasn't an error
                if let savedRecords = records {


                }

            }
        })
}

尽我所能,并根据我获得的信息。没有像database.saveRecords(复数)这样的实际功能。除非我错过了什么?我认为它并没有那么干脆和命名不同,但我认为必须有类似的东西。

每次我通过循环时都必须进行数据库调用,这似乎是非常低效的。

有人知道使用CloudKit 一次保存多个CKRecords 的方法吗?任何帮助都将不胜感激。提前谢谢!

【问题讨论】:

    标签: ios swift cloudkit ckrecord


    【解决方案1】:

    在循环中使用saveRecord 效率非常低。其实最好避免CKDatabase上的所有便捷方法,并使用适当的操作。

    要添加/修改/删除一条或多条记录,请使用CKModifyRecordsOperation。您可以通过一次操作添加/修改以及删除任意数量的记录。这将比使用saveRecord 高效得多。

    我建议您浏览CloudKit Framework Reference 以查看可用操作的完整列表。

    【讨论】:

    • 这正是我想要的!我忘记了我可以使用CKModifyRecordsOperation 来保存多条记录,我以前用过很多次来删除东西,但从来没有保存过。谢谢!
    【解决方案2】:

    要建立在 rmaddy 所说的基础上,这里有一个示例代码,如果您在“delete”参数中添加一个 CKRecords 数组(我在这里设置为 nil),它也适用于删除。这就是我的意思:

    let db = CKContainer.default().publicCloudDatabase
                
    let operation = CKModifyRecordsOperation.init(recordsToSave: recordsToSave, recordIDsToDelete: nil)
    operation.modifyRecordsCompletionBlock = { _, _, error in
        if let error = error{
            print(error.localizedDescription)
            let alert = UIAlertController(title: "Operation Failed", message: "There was a problem interacting with some records.", preferredStyle: .alert)
            let ok  = UIAlertAction(title: "Ok", style: .cancel, handler: nil)
            alert.addAction(ok)
            self.present(alert, animated: true, completion: nil)
        }
    }
    db.add(operation)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-03-18
      • 1970-01-01
      • 2014-08-27
      • 1970-01-01
      相关资源
      最近更新 更多