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

#1

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?

#2

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.

#3

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.

#4

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.+

#5

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:

#6

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

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

#7

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.

#8

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

#9

Have you considered localforage ?

1 Like
#10

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)

#11

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?

#12

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

1 Like
#13

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:

$localForage (localStorage/indexedDB) periodically cleared on iOS
#14

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.

#15

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.

#16

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?

#17

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)

#18

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.

#19

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
#20

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

Maybe I give SQLite a try.