繁体   English   中英

IDBDatabase.createObjectStore之后是否必须使用IDBObjectStore.transaction.oncomplete?

[英]Whether should IDBObjectStore.transaction.oncomplete be necessarily used after the IDBDatabase.createObjectStore?

我正在阅读MDN的这篇文章: Using IndexedDB

有两个示例,它们创建一个新的对象存储并在创建后添加数据。

第一个示例使用IDBObjectStore.transaction.oncomplete来确保在添加数据之前完成商店创建。

var objectStore = db.createObjectStore("customers", { keyPath: "ssn" });
// Use transaction oncomplete to make sure the objectStore creation is 
// finished before adding data into it.
objectStore.transaction.oncomplete = function(event) {
  var customerObjectStore = db.transaction("customers", "readwrite").objectStore("customers");
  customerData.forEach(function(customer) {
    customerObjectStore.add(customer);
  });
};

但是,当我看下面的示例时,这让我有些困惑:

var objStore = db.createObjectStore("names", { autoIncrement : true });
customerData.forEach(function(customer) {
  objStore.add(customer.name);
});

如您所见,数据是在没有事务的情况下添加的,因此,从理论上讲,第二个示例在尝试在对象存储就绪之前添加数据时会引起一些问题。

令人惊讶的是,这两个示例都可以工作

我想知道应该优先选择哪个人,为什么要优先选择它。 使用交易真的有必要吗?

使用第二个示例,不需要第一个示例。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM