Do I need to refresh a page to see if the Indexed DB was reset?

↘锁芯ラ 提交于 2019-12-11 18:14:35

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!