0

For some reason, when I restart my PhoneGap app - it looses the localStorage vales that were stored before! I'm saving them in the normal way:

localStorage.setItem("foo","value");

This stores it just fine. However, when you restart the app (or leave the device off for a random amount of time), it seems to randomly loose the data. I've found a heck of a lot of posts about this - but no definative answer on how to get it to be persistent in a PhoneGap Build WebView app,

Any suggestions are much welcomed!

This seems to be quite a common problem with WebView apps:

I can't find a solution that works with PhoneGap Build apps though

An actual example I'm using, is:

    var current_id = parseInt(currentId) + 1;
    localStorage.setItem("entry_"+current_id,save_string);
    localStorage.setItem("entryId",current_id);

..and then to extract it (not that this is important, as the problem is with the data going missing, and not with accessing it)

                for (var i = 0; i < localStorage.length; i++){
                    if (localStorage.key(i).match("entry_")) {
                        outputString += "\n" + localStorage.getItem(localStorage.key(i));
                    }
                }

I'm wondering if maybe upgrading from PhoneGap Build cli-5.2.0 to cli-6.0.0 may help. I will do this, and give it a whirl.

I guess another option, would be to use a SQL database to locally store the device (its just a bit trickier to setup, and means re-writing my code)

UPDATE: Not the ideal solution - but I have now moved the app over to use WebSQL for the app. It was a bit tricky to get the hang of (never used it before) - but seems to do the job, and shouldn't loose the data :)

Community
  • 1
  • 1
Andrew Newby
  • 4,941
  • 6
  • 40
  • 81
  • Updating to cli-6.0.0 should fix your issue, as it was fixed on cordova-android 5.1.1, that's the version included on cli-6.0.0 https://issues.apache.org/jira/browse/CB-10157 – jcesarmobile Apr 05 '16 at 14:38
  • @jcesarmobile - thanks for the update. I ended up having to convert the whole app to use WebSQL instead, as the localStorage was just too unreliable :( Good to know it should be fixed now though – Andrew Newby Apr 06 '16 at 10:19
  • and WebSQL isn't deleted? the bug was that the app is uninstalled and reinstalled, that should delete WebSQL too. – jcesarmobile Apr 06 '16 at 10:33
  • @jcesarmobile - ah ok, well maybe thats not the same bug :) It wasn't an issue of uninstalling/reinstalling... but just restarting the app (turning the device off, leaving it off for a bit, then restarting) – Andrew Newby Apr 09 '16 at 05:11
  • Can you tell me the android version where you tested? I would like to look into it and try to figure out if it's an android bug, a cordova bug or webview bug. – jcesarmobile Apr 09 '16 at 06:20
  • 1
    @jcesarmobile - it was on the latest version (6.0.1). Thanks! – Andrew Newby Apr 09 '16 at 08:15
  • Thanks, I'm trying to get a new android 6 phone – jcesarmobile Apr 09 '16 at 08:32

1 Answers1

0

EDIT

i tried it like this and it worked:

    var current_id = parseInt(currentId) + 1;
    localStorage.setItem("entry_"+current_id,save_string);
    localStorage.setItem("entryId",current_id);
/*
    //this is for checking, what is stored in localStorage
    console.log("length: " + localStorage.length);
    for(var i = 0; i < localStorage.length; i++) {
        console.log(localStorage.key(i));
    }
*/

    var myEntryIdFromStorage = localStorage.getItem("entryId");
    var myItem = localStorage.getItem("entry_" + myEntryIdFromStorage);


Old answer for clarification

How do you get your localstorage?

normally you should store items like you did:

var permanentStorage = window.localstorage;

permanentStorage.setItem("foo", "bar");

and get them back by initializing the permanentStorage the same way and:

//assuming you have permanentStorage in the same script file
//or else you have to initialize it again:
//var permanentStorage = window.localstorage;

var myItem = permanentStorage.getItem("foo");
console.log("myItem: " + myItem);

The method store item uses two parameters: the identifier and the data itself. Please check, that the identifier with which you store your data is the same as the one, with which you get it back.

Do you get any errors? Is the return (stored in my example in myItem) null or undefined or just an empty string? Does this fail in the browser or on the device?

You could clarify your question by providing more code or error messages!

Sunny Onesotrue
  • 152
  • 3
  • 13
  • I just do it as `localStorage.setItem("entry_"+current_id,save_string);`, and then use `localStorage.getItem("x")` to grab it back ... but this is what isn't saving properly :( (it saves it to start with, but if you turn the device off for a bit, and then turn it back on, the data has gone) – Andrew Newby Mar 24 '16 at 09:49
  • I found this old post, but I'm not sure how this can be done with the PhoneGap Build process (i.e in the JS): http://stackoverflow.com/questions/4157184/android-making-webview-domstorage-persistant-after-app-closed – Andrew Newby Mar 24 '16 at 09:50
  • Can you please edit your question and provide more code. Else it's hard to help you! – Sunny Onesotrue Mar 24 '16 at 09:52
  • - no problem. I have updated the OP to include a bit more. As I said, its pretty bog standard code... which is why I'm confused as to why its being lost – Andrew Newby Mar 24 '16 at 09:57
  • thanks! what is your localstorage length after you return to your app, when it was closed? is there nothing in it? because you store your items correctly, but your call "localStorage.key(i).match("entry_")" seems to be wrong. You never store an item with the identifier "entry_", but with the identifier "entry_"+currentId – Sunny Onesotrue Mar 24 '16 at 10:07
  • thats the weird thing - after starting the app, it has all the data in fine. It seems to be when the tablet is turned off for a longer amount of time (which is making it a sod to try and see if the bug gets fixed) – Andrew Newby Mar 24 '16 at 10:12
  • ok, this isn't clear from your original question...that the behaviour only occurs after the app has already launched and the tablet is turned off for a while... – Sunny Onesotrue Mar 24 '16 at 10:19
  • 1
    I've updated the question to be a bit more explicit in that area: *However, when you restart the app (or leave the device off for a random amount of time)* – Andrew Newby Mar 24 '16 at 10:21