localStorage.length check on resume/restart/update

348 views
Skip to first unread message

Akeman

unread,
May 13, 2012, 6:34:47 AM5/13/12
to phon...@googlegroups.com
Hi,

I have been going round in circles on this and I am sure that I am missing something obvious.

Using the following I check for whether the app has been run previously.

$('#page1').live('pageinit',function(event){   
 console.log("StartUp1a localStorage.length "+localStorage.length);
  if (localStorage.length<1)  

  code

I get an inconsistent result. I write 8 keys from the app and sometimes I get 8 and others I get 0 and the routine resets all data.

Am I doing something wrong or is this a bug?

Cheers

Tim

Akeman

unread,
May 13, 2012, 6:42:12 AM5/13/12
to phon...@googlegroups.com
Sorry should have said - Android 3.2, Cordova 1.6.0 and 1.7.0

Works fine and consistently in chrome


Akeman

unread,
May 13, 2012, 10:16:43 AM5/13/12
to phon...@googlegroups.com
I have also now checked it using deviceready, still does the same thing :(



Akeman

unread,
May 18, 2012, 11:03:48 AM5/18/12
to phon...@googlegroups.com
Still no joy on this - Have tried every variety of test for local storage and still get random nulls when I know there is content in localstorage.
 
I have now tested on iOS and Android browsers - works fine
 
Anyone else having problems reading from localStorage as the application loads?
 
 

Bryce Curtis

unread,
May 18, 2012, 1:39:17 PM5/18/12
to phon...@googlegroups.com
I've never tried using localStorage.length, but I check to see if a
localStorage variable is set.
> --
> You received this message because you are subscribed to the Google
> Groups "phonegap" group.
> To post to this group, send email to phon...@googlegroups.com
> To unsubscribe from this group, send email to
> phonegap+u...@googlegroups.com
> For more options, visit this group at
> http://groups.google.com/group/phonegap?hl=en?hl=en
>
> For more info on PhoneGap or to download the code go to www.phonegap.com

Herb

unread,
May 18, 2012, 2:20:11 PM5/18/12
to phonegap
On 1.6.1. and Android 2.2,

The syntax I use is window.local.storage.length

Here is a little thing I slip in sometimes for checking local storage:

function audit() {
var temp = "";
for ( var i = 0; i < window.localStorage.length; i++) {
temp += window.localStorage.key(i) + "=";
temp += window.localStorage.getItem(window.localStorage.key(i)) +
"/ /";
}
alert(temp);

Libby

unread,
May 18, 2012, 5:06:41 PM5/18/12
to phon...@googlegroups.com
Akeman,

I use a similar scheme to check for app first-run, and have
never seen what you describe (Android 2 & 3).

If can repro it often enough, what key names do you see if
running Herb's code?

My approach is conservative: I always use and look for specific
key names.

Libby

Akeman

unread,
May 19, 2012, 3:45:36 AM5/19/12
to phon...@googlegroups.com
Hi Herb, Libby and Bryce,
 
Thanks for this input.
 
As the app starts it either reads as expected the 8 variables or doesn't find any localStorage values at all.
 
On another restart it may pick up the variables.
 
I have now used localStorage.length and window.localStorage.getItem in many ways.
 
Maybe I am writing something to the storage it doesn't like - although I am only writing string pairs, some contain a lot of data.
 
This maybe one of those where I take it right back to nothing and build one step at a time to find it as I feel I have tried everything I can think of!
 
Cheers
 
Tim

Herb

unread,
May 19, 2012, 6:22:28 AM5/19/12
to phonegap
Tim,

The bit of code I posted works reliably, so you may need to build
everything up from zero.

If it works sometimes, then it could be a timing/load issue. Are you
waiting for deviceready?

The first thing I would do is uninstall the app and reboot the device.

Herb

Akeman

unread,
May 23, 2012, 6:16:57 AM5/23/12
to phon...@googlegroups.com
Hi All,

Still battling this one.

I am running with Cordova 1.7.0 and using an HTC One X Android 4.0.3  for testing.

Using onDeviceReady, I read a variable that I set. I get three different values for this variable. 

1. null
05-23 10:39:38.208: D/CordovaLog(31017): file:///android_asset/www/scripts/akCordova.js: Line 18 : onDeviceReady
05-23 10:39:38.208: D/CordovaLog(31017): file:///android_asset/www/scripts/akCordova.js: Line 24 : onDeviceReady localStorage.localStorage.getItem('rstp1')  null

2. the actual value on the phone
05-23 10:38:17.604: D/CordovaLog(28929): file:///android_asset/www/scripts/akCordova.js: Line 18 : onDeviceReady
05-23 10:38:17.604: D/CordovaLog(28929): file:///android_asset/www/scripts/akCordova.js: Line 24 : onDeviceReady localStorage.localStorage.getItem('rstp1')  |22/05 start||1337710897765|1337712668121||||||1337712657471,l;1337712660474,l;1337712667043,l;1337712668121,e;||22/05 next||1337712677508|1337720568386||||||1337717881783,p;1337717885196,lp;1337720211715,l;1337720478347,l;1337720568386,e;||22/05 bed||1337720578636|0||||||

3. my default - this is written when the routine finds a null
05-23 10:57:32.969: D/CordovaLog(15611): file:///android_asset/www/scripts/akCordova.js: Line 18 : onDeviceReady
05-23 10:57:32.969: D/CordovaLog(15611): file:///android_asset/www/scripts/akCordova.js: Line 24 : onDeviceReady localStorage.localStorage.getItem('rstp1')  |simple timing||0|||||||

This is the record of the data coming from local Storage
10:48 Correct
10:49 Correct
10:51 simple
10:51 Correct
10:52 Correct
10:52 Correct
10:52 null
10:53 Correct
10:53 Correct
10:54 Correct
10:54 null
10:55 Correct
10:55 null
10:56 Correct
10:56 Correct
10:56 simple
10:56 Correct
10:57 null
10:57 Correct
10:58 simple

This is before any other actions are taken within the phone.

This is my start up script

function onLoadCordova() {      
console.log("onLoadCordova"); 
document.addEventListener("deviceready", onDeviceReady, false);    
}

and then 

function onDeviceReady() {            
console.log("onDeviceReady");   
// Register the event listener   
document.addEventListener("menubutton", onMenuButtonDown, false); 
console.log("onDeviceReady localStorage.localStorage.getItem('rstp1')  "+localStorage.getItem('rstp1')); // read variable to console before other actions
initialize();  // set up the app
}     

From what I can see the app reads one of three values with a bias towards the correct one - what doesn't seem to happen is a consistent read.

Any ideas from the floor?

Best

Tim

Herb

unread,
May 23, 2012, 8:27:49 AM5/23/12
to phonegap
Tim,

1.

Here is what jumps out at me as suspicious. Of course, you may be
getting from point A to point B in a different way that is unfamiliar
to me. You may very well be using an A to B route that should work,
but exposes some obscure weakness in Phonegap. My list:

-the syntax I use is
.
.

var x="abc";
window,localStorage.setItem("savex",x);
.
.
var y= window,localStorage.getItem("savex");
.
.

You are missing the 'window.' See the API documentation.

- I assume that your onLoadCordova() function is called in the body
tag of index.html?

-I would like to see the setItem code to check if you are doing that
part correctly.

2. My general approach to these sorts of challenges is to blame
Phonegap, Eclipse, the phone, etc. So:

-uninstall the app from your phone. Re-install is not good enough.
localStorage survives a re-install and then any change in the keys in
localStorage leads to unpredictable results

-reboot the phone. Why not, can't hurt.

-reboot your desktop and start up eclipse. I assume that you are
using eclipse. I would suggest uninstalling and re-installing
eclipse, but that is too cruel.

-Now, sing God Save the Queen, then install a fresh copy of the app on
your device.

3.

If you sang the national anthem and it still does not work, I suggest
commenting out all the meat of your code and inserting a simple
localStorage example that pops up an alert and see if that works.

4.

So, if the simple example works, you can only blame some ahem
peculiarity in your code. Build it back up again in chunks by
uncommenting various sections one at a time if you can.

Good Luck

Herb


On May 23, 6:16 am, Akeman <tim.but...@akemansolutions.com> wrote:
> > Hi All,
>
> Still battling this one.
>
> I am running with Cordova 1.7.0 and using an HTC One X Android 4.0.3  for
> testing.
>
> Using onDeviceReady, I read a variable that I set. I get three different
> values for this variable.
>
> 1. null
> 05-23 10:39:38.208: D/CordovaLog(31017):
> file:///android_asset/www/scripts/akCordova.js: Line 18 : onDeviceReady
> 05-23 10:39:38.208: D/CordovaLog(31017):
> file:///android_asset/www/scripts/akCordova.js: Line 24 : onDeviceReady
> localStorage.localStorage.getItem('rstp1')  null
>
> 2. the actual value on the phone
> 05-23 10:38:17.604: D/CordovaLog(28929):
> file:///android_asset/www/scripts/akCordova.js: Line 18 : onDeviceReady
> 05-23 10:38:17.604: D/CordovaLog(28929):
> file:///android_asset/www/scripts/akCordova.js: Line 24 : onDeviceReady
> localStorage.localStorage.getItem('rstp1')  |22/05
> start||1337710897765|1337712668121||||||1337712657471,l;1337712660474,l;133 7712667043,l;1337712668121,e;||22/05
> next||1337712677508|1337720568386||||||1337717881783,p;1337717885196,lp;133 7720211715,l;1337720478347,l;1337720568386,e;||22/05

Akeman

unread,
May 24, 2012, 6:24:31 AM5/24/12
to phon...@googlegroups.com
Hi All, 

Cracked it and and here are some of the learnings.

1. Big thank you to all that have beared with me through this.

2. localStorage is stable and persistent in 1.7.0 provided other code does not interfere on 2.3.3 and 4.0.3 on Android.

3. It was Admob starting at the same time as the boot that made localStorage produce random results, I followed the recently updated and now very helpful article on http://wiki.phonegap.com 


"If you are using localStorage within your PhoneGap / Cordova app, you will want to use Option 2 for the actual Java Code.  Loading Admob on demand will clear localStorage for some reason.  All the other steps are the same."


4. Placing your loading code into onDeviceReady delays it running until all of Cordova has loaded in my case that means the dynamic scripts don't run for a second causing a flash.


In order to effect the page as it is ready, I used the jQuery mobile $(document).bind('pageinit',function(event) to trigger the initialize script and this has been reliable including constant updates from eclipse and forced restarts from Android settings.


I hope this helps some others, a few more grey hairs here and a smile as the sun is now out and I can get on with the rest of the app.


Cheers


Tim





 

Dave I

unread,
Dec 19, 2012, 1:45:03 PM12/19/12
to phon...@googlegroups.com
Tim could you please post your jqm initialization code even your droidgap class I am desperately battling with this and could do an early christmas present

Dave I

unread,
Dec 19, 2012, 1:45:05 PM12/19/12
to phon...@googlegroups.com

Oscar de Dios

unread,
Dec 19, 2012, 5:31:12 PM12/19/12
to phon...@googlegroups.com, tim.b...@akemansolutions.com
Tim I'm fighting with this too, I've followed the wiki instructions but the ads doesn't appear.

Thanks
Reply all
Reply to author
Forward
0 new messages