繁体   English   中英

我似乎从IndexedDB索引中获取了一个脏读。 这可能吗?

[英]I seem to be getting a dirty read from an IndexedDB index. Is this possible?

我有一些js使用readwrite事务对IndexedDB(在Chrome中)执行put,然后使用索引和readonly事务立即从同一indexedDB对象库查询。 有时,我得到的结果不包括我的put和其他时间的变化。 在IndexedDB中是否可以预期这种脏? 有没有办法避免它?

也许这是因为我使用了两个不同的txns并且应该只使用一个(原因是这些调用实际上是api的一部分,将put和查询分成不同的api调用,每个调用都有自己的txns)? 不过,似乎第一个txn应该在我的第二个开始之前完成并提交。

我的伪代码看起来像这样:

var txn = idb.transaction([DOC_STORE], "readwrite");
var putRequest = txn.objectStore(DOC_STORE).put(myDoc);
putRequest.onsuccess = function (e) {
    var txn2 = idb.transaction([DOC_STORE], "readonly");
    var store = txn2.objectStore(DOC_STORE);
    var anotherRequest=store.index.openCursor();
    .... walk the cursor here. Sometimes I don't see my changes from my put
};

您必须等待写事务完成。 它比请求成功事件晚。

var txn = idb.transaction([DOC_STORE], "readwrite");
var putRequest = txn.objectStore(DOC_STORE).put(myDoc);
txn.oncomplete = function (e) {
    var txn2 = idb.transaction([DOC_STORE], "readonly");
    var store = txn2.objectStore(DOC_STORE);
    var anotherRequest=store.index.openCursor();
    .... walk the cursor here. You will see see your all changes from your put
};

或者,您可以在同一事务中使用请求成功。

var txn = idb.transaction([DOC_STORE], "readwrite");
var putRequest = txn.objectStore(DOC_STORE).put(myDoc);
putRequest.onsuccess = function (e) {
    var store = txn.objectStore(DOC_STORE);
    var anotherRequest=store.index.openCursor();
    .... walk the cursor here. You will see see your all changes from your put
};

暂无
暂无

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

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