如何在CloudKit JS v2中保存记录

时间:2017-09-20 18:46:24

标签: javascript cloudkit cloudkit-web-services

使用CloudKit v1,您可以保存如下记录:

function demoSaveRecord(recordName,recordChangeTag,zoneName,name,location,asset) {

  var container = CloudKit.getDefaultContainer();
  var privateDB = container.privateCloudDatabase;

  var record = {
    recordType: 'Items',
    fields: {
      name: { value: name }, ...
    }
  };

  return privateDB.saveRecord(record,options)
    .then(function(response) {
      if (response.hasErrors) {
        // Handle the errors in your app.
        throw response.errors[0];

      } else {
        // It worked
        var createdRecord = response.records[0];
      }
    });
}

但在CloudKit JS v2中,他们删除了.saveRecord选项。

所以现在我被卡住了,因为我无法弄清楚如何在v2中保存记录。 如果有人可以帮助我,或者可以向我展示v2的良好文档,请提供帮助。

CloudKit JS API差异v2 https://developer.apple.com/library/content/releasenotes/General/CloudKitJS_v2_APIDiffs/JavaScript/CloudKitJS.html

1 个答案:

答案 0 :(得分:0)

显然你必须改为使用.newRecordsBatch().create(record).commit()

在我的例子中,它看起来像这样:

function demoSaveRecord(recordName,recordChangeTag,zoneName,name,location,asset) {

  var container = CloudKit.getDefaultContainer();
  var privateDB = container.privateCloudDatabase;

  var record = {
    recordType: 'Items',
    fields: {
      name: { value: name }, ...
    }
  };

  return privateDB.newRecordsBatch().create(record).commit()
    .then(function(response) {
      if (response.hasErrors) {
        // Handle the errors in your app.
        throw response.errors[0];

      } else {
        // It worked
        var createdRecord = response.records[0];
      }
    });
}

您可以使用相同的方法来创建,更新和删除这样的记录:

myDatabase.newRecordsBatch()
    .create(someRecord)
    .update(someOtherRecord)
    .delete(aThirdRecord)
    .commit()

可在此处找到文档: https://developer.apple.com/documentation/cloudkitjs/cloudkit.recordsbatchbuilder