gpt4 book ai didi

indexeddb - 如何在 IndexedDB 的一个事务中放置多个请求

转载 作者:行者123 更新时间:2023-12-03 06:44:54 25 4
gpt4 key购买 nike

我的代码如下:

...
var f1 = function(trans) {
var store = trans.objectStore('ObjectStore');
store.clear();
};
var f2 = function(trans) {
var store = trans.objectStore('ObjectStore');
store.add({id: 1, data: 'text'});
};
...
var trans = DB.connection.transaction(['ObjectStore'], IDBTransaction.READ_WRITE);
trans.onerror = function() { alert('ERROR'); };
trans.oncomplete = function() { alert('DONE'); };
...

我们遇到的问题是,我在 clear 后立即收到 DONE 警报,并且在第二个请求时出现异常。

是否可以“重用”IndexedDB 中的事务?

UPD:我发现上面的代码在最新的 Chromium nightly build 中可以按照我的预期工作。

最佳答案

According to Jonas Sicking是 IndexedDB 的 Mozilla 开发者和联合规范编写者,当最后一个回调触发时,就会提交事务。因此,为了保持事务处于事件状态,您应该能够通过连续的回调重用它。

您在上面使用了 oncomplete,但 onsucess 也应该同样有效。

您可以找到事务对象作为回调返回的请求对象的属性。

The following sentence isn't correct "Transactions today auto-commit when the transaction variable goes out of scope and no more requests can be placed against it".

Transaction never automatically commit when a variable goes out of scope. Generally they only commit when the last success/error callback fires and that callback schedules no more requests. So it's not related to the scope of any variables.

The only exception to this is if you create a transaction but place no requests against it. In that case the transaction is "committed" (whatever that means for a transaction which has no requests) as soon as you return to the event loop. In this scenario you could technically "commit" the transaction as soon as all references to it go out of scope, but it's not a particularly interesting use case to optimize.

基于 spec example在下面,您应该能够在 evt.transaction 中找到事务对象,并且您可以执行新事务并添加新的 onsuccess 事件监听器。

 var request = indexedDB.open('AddressBook', 'Address Book');
request.onsuccess = function(evt) {...};
request.onerror = function(evt) {...};

关于indexeddb - 如何在 IndexedDB 的一个事务中放置多个请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10484965/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com