Can You Use JavaScript localStorage as a Database for Your Apache Cordova or PhoneGap Application?

Over the past few months I’ve been programming primarily using Apache Cordova, and currently happen to be starting a new project which will require a bit of data persistence. I know that I can probably ship the production version of the app extremely quickly if I go with simple JavaScript localStorage for the in-app database. However, there are some downsides to this, and that’s what I’d like to talk about in this blog post.

Before digging into this topic tonight, I knew that data stored using localStorage may not be as persistent as other options. But what I didn’t know, was how persistent is it? Will a user clearing his or her web browser cache cause my app to lose data? What about clearing cached data for all applications under settings? How about specifically clearing cached data for my app specifically? Or clearing all data for my app specifically? Well now after a little experimentation and research, I think I have a pretty good answer!

  1. Clearing “browsing data” on Chrome for Android will not cause your Cordova app to lose localStorage data.
  2. Clearing “cached data” for all apps under Settings on Android will not cause your Cordova to lose localStorage data either.
  3. Selecting the “clear cache” option for your app specifically will not cause your Cordova app to lose data.
  4. SELECTING THE “CLEAR DATA” OPTION FOR YOUR APP SPECIFICALLY WILL CAUSE YOUR CORDOVA APP TO LOSE DATA.

So there you have it. There are the results of my non-scientific investigation into JavaScript localStorage and Apache Cordova. Your Cordova app’s data should be safe using localStorage as long as users do not go out of their way and select the “clear data” option for your app under Settings>Storage>Internal-Shared-Storage>Apps>YourCordovaApp. If you happen to be developing an Apache Cordova or PhoneGap application for Android, choosing JavaScript localStorage for your in-app database should be fine.

With that being said… JavaScript localStorage IS NOT A VIABLE OPTION FOR iOS APPLICATIONS. I’ll repeat that again… JavaScript localStorage IS NOT SUITABLE FOR iOS APPLICATION DEVELOPMENT WITH APACHE CORDOVA OR PHONEGAP.

Saved that abrupt zinger for the end huh?!? Yup, there’s the plot twist. Turns out, iOS will clear out your localStorage whenever it feels like it (apparently whenever memory is low). So if it isn’t a big deal if your localStorage gets cleared out, then you can go ahead and use it for iOS application development as well. But if data persistence is a really important feature of your app, make sure to stay clear of JavaScript localStorage when developing iOS applications with Apache Cordova or PhoneGap.

Things were looking good there for a moment, right? JavaScript localStorage for the win! For most use cases localStorage is still probably okay for iOS. However, for this latest untitled side project data persistence is going to be extremely important. So for Version 0, it looks like I’m just going to be rolling out on Android. Will have to tackle iOS another day using something a little more sound like the cordova-sqlite-storage plugin. Maybe that will make for an appropriate follow up post; // the end

 

topherPedersen