我似乎正在从IndexedDB索引中读取脏内容.这可能吗? [英] I seem to be getting a dirty read from an IndexedDB index. Is this possible?

查看:62
本文介绍了我似乎正在从IndexedDB索引中读取脏内容.这可能吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些js使用读写事务将其放入IndexedDB(在Chrome中),然后立即使用索引和只读事务从同一indexedDB对象存储中进行查询.有时,我得到的结果不包括我的推杆所做的更改,而其他时候则不包含.在IndexedDB中可以预料到这种肮脏的情况吗?有办法避免吗?

I have some js that does a put to IndexedDB (in Chrome) using a readwrite transaction, then immediately queries from the same indexedDB objectstore using an index and a readonly transaction. Sometimes, the results I get back do not include the changes from my put and other times they. Is this sort of dirty ready to be expected in IndexedDB? Is there a way to avoid it?

也许是因为我正在使用2个不同的txns,而应该只使用一个(原因是这些调用实际上是api的一部分,该API将puts和query分为每个都有自己的txns的不同api调用)?不过,似乎第一个txn应该在我的第二个txn开始之前完成并提交.

Perhaps it's because I'm using 2 different txns and should be using just one (the reason for that is these calls are actually part of an api that separates puts and queries into different api calls that each have their own txns)? Still, seems like the first txn should be done and committed before my second one starts.

我的伪代码如下:

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
};

推荐答案

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

You have to wait for the write transaction to complete. It comes later than request success event.

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
};

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

Alternatively, you can use request success in same transaction.

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
};

这篇关于我似乎正在从IndexedDB索引中读取脏内容.这可能吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆