1

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);
    }
}
the_moon
  • 553
  • 1
  • 7
  • 21

1 Answers1

3

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();
            };
Community
  • 1
  • 1
dumbmatter
  • 9,351
  • 7
  • 41
  • 80
  • Thanks Jeremy. I didn't know that. Nevertheless, the same is happening again. The oncomplete event is triggered and the DB was cleared (that's really weird)... Again, I only see that the DB was cleared after I reload the page... How could this be possible? The same is happening is a delete a patient with his id (http://stackoverflow.com/questions/20078724/delete-method-is-not-working-for-indexed-db-html5-it-returns-success-but-the/20085722?noredirect=1#20085722) – the_moon Nov 20 '13 at 13:30
  • I got it! The problem was that I need to use the oncomplete event to update the UI... Also, I have a minor problem in my update function. Thank you! – the_moon Nov 20 '13 at 18:14