我是否需要刷新页面以查看索引数据库是否已重置? [英] Do I need to refresh a page to see if the Indexed DB was reset?

查看:214
本文介绍了我是否需要刷新页面以查看索引数据库是否已重置?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我开始使用HTML 5的索引数据库,但我获得了一些奇怪的结果。第一个是我试图清除我的数据库,但我只看到它被重置,如果我刷新该网站。这是怎么回事?
我看过其他的示例代码,它不会以这种方式发生。



onsuccess被调用,但是更新方法显示的DB是和以前一样......

这是我的重置函数:

  function resetDB()
{
try
{
if(localDatabase!= null&& localDatabase.db!= null)
{
var store = localDatabase.db.transaction(patients,readwrite)。objectStore(patients);
$ b $ store.clear()。onsuccess = function(event)
{
alert(患者数据库清除);
update_patients_stored();
};
}
}
catch(e)
{
alert(e);
}
}


解决方案

onsuccess 可以在数据库中实际更新结果之前触发(请参阅此答案所以如果 update_patients_stored 从数据库中读取,它仍然可以看到旧数据,如果你使用一个事务的 oncomplete ,那么你就不会遇到这个问题。



如果这确实会导致你的问题,那么这将解决它:

  var tx = localDatabase.db.transaction(patients,readwrite); 

tx.objectStore (患者)clear();

tx.oncomplete =函数(事件)
{
alert(患者数据库清除);
update_patients_stored( );
};


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

解决方案

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

这篇关于我是否需要刷新页面以查看索引数据库是否已重置?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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