2013-03-06 40 views
0

我有一些js使用readwrite事务处理IndexedDB(在Chrome中),然后立即使用索引和只读事务从相同的indexedDB对象存储中查询。有时候,我得到的结果不包括我的投入和其他时间的变化。在IndexedDB中是否有这种脏东西?有没有办法避免它?我似乎从IndexedDB索引中获取脏读。这可能吗?

也许是因为我使用了2个不同的TXT并且应该只使用一个(原因是这些调用实际上是将放入和查询分离为不同的API调用的api的一部分,每个调用都有自己的TXT) ?尽管如此,第一个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 
}; 

回答

2

你必须等待写入事务完成。它晚于请求成功事件。

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

IDBTransaction.oncomplete为我解决了它。谢谢! – Category6 2013-03-06 14:26:06

相关问题