问题描述
在下面的getCursor_
函数中,请说明如何确定IndexedDb
是否已打开,以及是否不能重新运行它. getCursor_
正常运行.但是,由于所有这些调用都是异步的,因此在数据库完成打开之前执行时,该函数将失败.
In the getCursor_
function below, please explain how I could determine if IndexedDb
has opened and if not re-run function once it has? The getCursor_
runs correctly. However, since all of these calls are asynchronous, the function fails when executing before the database has completed opening.
此代码在单独的过程中执行:
var ixDb;
var ixDbRequest;
ixDbRequest = window.indexedDB.open(dbName, dbVersion);
ixDbRequest.onsuccess = function (e) {
ixDb = ixDbRequest.result || e.result;
};
除非ixDbRequest
尚未完成执行,否则下面的getCursor_
函数可以正常工作.我想出了如何测试的方法,但是不确定在打开的数据库请求仍在运行的实例中如何等待.
The getCursor_
function below works fine unless ixDbRequest
has not completed execution. I figured out how to test for that, but I am not sure how to wait in the instance where the open database request is still running.
function getCursor_(objectStoreName) {
if (!ixDb) {
if (ixDbRequest) {
// "Database is still opening. Need code to wait or re-run
// once completed here.
// I tried the following with no luck:
ixDbRequest.addEventListener ("success",
getCursor_(objectStoreName), false);
return;
}
}
var transaction = ixDb.transaction(objectStoreName,
IDBTransaction.READ_ONLY);
var objectStore = transaction.objectStore(objectStoreName);
try{
var request = objectStore.openCursor();
return request;
}
catch(e){
console.log('IndexedDB Error' + '(' + e.code + '): ' + e.message);
}
}
更新如下:
@Dangerz的回答无疑帮助我走上了正确的道路.但是,由于函数调用是异步的,因此一旦成功"事件最终触发后,我还不得不添加一个回调,以便实际上能够使用游标,并且我能够获得所请求的IndexedDb游标.最终的工作功能如下(略作重构以消除"if(!ixDb)"上方的负面逻辑.如果有人认为有改进的余地,我完全愿意提出建议!
The answer from @Dangerz definitely helped put me on the right track. However, since the function call is asynchronous, I also ended up having to add a callback in order to actually be able to use the cursor once the "success" event finally fired, and I was able to get the requested IndexedDb cursor. The final working function is below (re-factored slightly to remove the negative logic above "if(!ixDb)" . I am totally open to suggestions, if anyone sees room for improvement!
//****************************************************************************
//Function getCursor - Returns a cursor for the requested ObjectStore
// objectStoreName - Name of the Object Store / Table "MyOsName"
// callback - Name of the function to call and pass the cursor
// request back to upon completion.
// Ex. getCursor_("ObjectStore_Name", MyCallBackFunction);
// Function MyCallBackFunction(CursorRequestObj) {
// CursorRequestObj.onsuccess =
// function() {//do stuff here};
// }
//****************************************************************************
function getCursor_(objectStoreName, callback) {
//The the openCursor call is asynchronous, so we must check to ensure a
// database connection has been established and then provide the cursor
// via callback.
if (ixDb) {
var transaction =
ixDb.transaction(objectStoreName, IDBTransaction.READ_ONLY);
var objectStore = transaction.objectStore(objectStoreName);
try{
var request = objectStore.openCursor();
callback(request);
console.log('ixDbEz: Getting cursor request for '
+ objectStoreName + ".");
}
catch(e){
console.log('ixDbEz Error' + ' getCursor:('
+ e.code + '): ' + e.message);
}
}
else {
if (ixDbRequest) {
ixDbRequest.addEventListener ("success"
, function() { getCursor_(objectStoreName, callback); }
, false);
}
}
}
推荐答案
将addEventListener行更改为:
Change your addEventListener line to:
ixDbRequest.addEventListener ("success", function() { getCursor_(objectStoreName) }, false);
这篇关于什么时候可以告诉我已在indexedDB中打开连接?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!