Skip to content Skip to sidebar Skip to footer

Atomic Operation On Window.localstorage

Is this possible to do when setting 2 or more values? I am using phonegap and concerned that the user could kill the app while before it finishes setting both values. I realize tha

Solution 1:

By default, no, there is no locking mechanism. However, you may want to check out this question also, and this site that it references. You would be better off with a SQL transaction if persistence of those values in an atomic fashion is vital to your program.

Solution 2:

Protect the writes:

localStorage.setItem("writing", "1");

localStorage.setItem("k1", "v1");
localStorage.setItem("k2", "v2");
...

localStorage.removeItem("writing");

Then, when loading the DB:

if (localStorage.writing != undefined)
{
    treat the DBas corrupted
}

Post a Comment for "Atomic Operation On Window.localstorage"