问题
I started working with Indexed DB for HTML 5 but I am obtaining some strange results. The first one is that I try to clear my database but I only see that it was reset if I refresh the site. Is that how it should be? I have seen other sample codes which it does not happen in this way.
The onsuccess is called but the DB shown, by the update method, is the same that was before...
Here is my reset function:
function resetDB()
{
try
{
if (localDatabase != null && localDatabase.db != null)
{
var store = localDatabase.db.transaction("patients", "readwrite").objectStore("patients");
store.clear().onsuccess = function(event)
{
alert("Patients DB cleared");
update_patients_stored();
};
}
}
catch(e)
{
alert(e);
}
}
回答1:
onsuccess
can fire before the results are actually updated in the database (see this answer to a question I asked here. So if update_patients_stored
is reading from the database, it might still see the old data. If you use a transaction's oncomplete
, then you won't have that problem.
If that is indeed causing your issue, then this will fix it:
var tx = localDatabase.db.transaction("patients", "readwrite");
tx.objectStore("patients").clear();
tx.oncomplete = function(event)
{
alert("Patients DB cleared");
update_patients_stored();
};
来源:https://stackoverflow.com/questions/20078335/do-i-need-to-refresh-a-page-to-see-if-the-indexed-db-was-reset