Software Archive
Read-only legacy content
17060 Discussions

localStorage is lost after restart

Alper_B_
New Contributor I
2,856 Views

Hello,

As a seperate post I'd like to send here, as it is a bit different than the other thread.

I use HTML5 + Cordova Blank project plus AngularJS. Without Cordova build the local storage functionality works fine. However, with the Cordova build, when I set a value at localStorage it works, but when I restart the APP, the values within the LocalStorage is lost.

I have tried both in emulator and on device. The result is same. XDK version 1995

I have tried both window.localStorage and just localStorage.

Here are the code snippets I use:

this.getUserToken = function () {

        try
        {
        return JSON.parse(localStorage.getItem("userToken"));
        }
        catch (err)
        {
            alert(err.message);
            return null;
        }
    };

    this.setUserToken = function (token) {
        return localStorage.setItem("userToken", JSON.stringify(token));
    };

Any help would be super appreciated.


 

0 Kudos
5 Replies
David_B_22
Beginner
2,856 Views

I tried just putting this basic code in an init-app.js:

    var thisisit = localStorage.getItem('thing');
    
    alert(thisisit);
    
    localStorage.setItem('thing','tostore');

and it works fine on emulator and device debug.  (first time null displayed, second time 'tostore'.  maybe double check you're not trying to store a null json token value?

 

0 Kudos
Alper_B_
New Contributor I
2,856 Views

Hi David,

Thank you for the prompt reply.

Yes, I'm sure that I'm not writing null. Because right after writing, i'm displaying the latest status of localStorage and it shows the right content. Right after I restart the app, the localstorage returns null again. Code logic is very simple, I tested carefully many times.

Interesting is, intel.xdk.cache.setCookie and getCookie also fails. Now I'm taking a shot using Cordova FileSystem tor read and write my content, because I'm under deadline pressure. 

Best regards,
Alper

0 Kudos
PaulF_IntelCorp
Employee
2,856 Views

To make any of the intel.xdk namespace functions work make sure you've got a reference to intelxdk.js in your index.html file (before the reference to cordova.js) and have selected the appropriate plugin from the plugins list.

0 Kudos
Dragan_T_
Beginner
2,856 Views

Hi Alper,

I hope you have managed to resolve your issues. If you are still struggling, here is my 2 cents..
I had some troubles with localStorage long time ago, but it turned out it was my fault - even I was using very simple code.
It had to do with me actually deleting the object value unintentionally by using references and not copies of objects where I should not.
It might not be a case, but can be cause of trouble sometimes, and is easily missed.

Second, a hint - are you using the debug windows in your XDK Emultor? - It can show the content of localStorage so you can follow what and when is stored/deleted.

In my last project, I was using AngularJS and ngStorage (actually a wrapper for localStorage, and it is working fine in XDK - tested before the latest 1995 update) on both Emulator and Device (using Crosswalk).

Best Regards

0 Kudos
Alper_B_
New Contributor I
2,856 Views

Hello,

You guys were all right. That was a programming mistake from us and nothing to do with XDK.

Thanks a lot!

 

0 Kudos
Reply