Localstorage - is it cleared after app restarts/ periodically in iOS?

There are conflicting info about this all over -

and others!

What is the current authorititative status of this?

No mention about how long the data will persist here:
http://learn.ionicframework.com/formulas/localstorage/

Is localstorage persistent with the latest iOS / Android?

of course, you can use it directly $window.localStorage or with some angular directive like angular-localstorage which give you abstraction layer to use localStorage.

That stackoverflow post is very old. LocalStorage seems to work fine in my tests, although it would be interesting to find out exactly where the data is stored on disk.

Exactly. And how long / till what event is it guaranteed to be available? How do we find out?

I made a test app -and it reports that the data in localStorage is ā€œsometimesā€ not available - not sure what event causes it. An app restart / phone restart / crash?

So wanted to know the official behaviour.

I am on the latest ionic / and iOS 8.+

Iā€™ve never seen the localStorage not being available so far. It should be persistent, restarting the app or phone shouldnā€™t cause it to be deleted. Uninstalling the app will delete it. If it behaves differently Iā€™d consider that to be a bug in Cordova.

Hereā€™s the ā€œauthoritativeā€ source on how it behaves on iOS: CDVLocalStorage.m. :wink:

And this?
https://code.google.com/p/chromium/issues/detail?id=481380

In Android 5.0 this problem appears in app ionic. Not persistant.

My app worked fine in an emulator with Android 5.1 last time I tried, but good to know about that issue in Chrome. I may decide to use SQLite instead of local storage.

That said, we canā€™t blame Ionic for bugs in Chrome or other projects.

My app in emulator 5.0 work, in real device not.

Have you considered localforage ?

1 Like

This seems like a pretty big bug, I have an app for android with crosswalk that heavily relies on localStorage data.

Hope this doesnā€™t enter into crosswalk(using version 11.xx), will have to check every time I update it. For the moment localStorage stores just fine for several months already (updates included)

For Android there is a comment at https://code.google.com/p/chromium/issues/detail?id=479767 that suggests that it is ā€œjustā€ the last entry made to localStorage that can potentially get lost.

Has anyone tried a workaround whereby a dummy / sacrificial last entry is made leaving the other entries intact?

I wish Objective C was not that cryptic to read :confused:

1 Like

I have an app in the App Store for more than a year now that uses LocalStorage and never had any problems with that. Data was never lost, so I wouldnā€™t worry about that.

Update: I now actually have experienced problems due to low memory and iOS clearing the localStorage because of thatā€¦

You have other options as well, like IndexedDB (only use that on Android), WebSQL, SQLite, or use PouchDB, which will encapsulate all of them. It so happens that I just wrote a blog post about using Ionic with PouchDB :wink:

Objective-C is exactly why Swift became popular :smiley:

BTW Ive not seen any problems with using local storage on iOS and Android. But it is really only designed to store simple stuff (credentials, preferences, etc). For more complex data, youā€™re better off with a real database. We are using Couchbase Lite locally and have a built an API on the server side that stores to a cluster of Couchbase servers. Syncing is handled by Couchbaseā€™s builtin replication (two-way sync for ā€˜freeā€™ essentially) to Coucbase Sync Gateway.

So I figured out what that ā€œeventā€ was.

My understanding (and that matches the experiments with my test app) is:

a) LocalStorage works well (tested on iOS 8.3 / iphone 5) - not randomly cleared.
b) However, there is one corner case in which I have seen that to be cleared repeatedly - and that is when the phone runs out of space. So my phone was very low on disk space - and periodically the icons on the phone face would go ā€œCleaningā€¦ā€ (where the app name is written) - and when that status showed up against my app - that meant that the localstorage has been wiped off.

I just made a 2 gigs of space on the device - and have had no problems of localstorage ever since.

Hope this helps.

Were hitting this issue with our app. LocalStorage will wipe when the disk space on the device gets low. Did you find a solution to this?

There seems to be a bug with chromeview affecting those that use crosswalk in their apps that causes the localstorage to be reset each time the app restarts. (https://code.google.com/p/chromium/issues/detail?id=481380)

The problem is now resolved and I can confirm that it is fixed when using the latest version of crosswalk (14.42.334.0)

SO that is expected behavior.

I am not sure what android has available but for iOS there is NSUserDefaults that persist until the user wipes the app off the phone. Iā€™m sure there is a plugin for that, and one equivalent for android, i just havenā€™t looked.

Iā€™m also working on an ionic app and during testing phase on my phone I had no worries about local storage which was persistent.

Once deployed on Apple App Store some users complained about losing data time to time.

So my conclusion is that the local storage is not reliable at 100% because of iOS cleaning some apps or some strange behavior of safari in private mode.

Conclusion : dot not use local storage for sensitive data that you want to persist.

Personally I ended up using localforage and indexdb driver.

1 Like

With Android > 5 I noticed that localStorage is cleared when one closes the app via task manager.

Maybe I give SQLite a try.