Device-Bugs icon indicating copy to clipboard operation
Device-Bugs copied to clipboard

(iOS)(Cordova) localStorage can get unexpectedly cleared when device storage is low

Open jakub-g opened this issue 10 years ago • 11 comments

Platforms:

Cordova apps on iOS, Safari on iOS

Summary:

localStorage might get cleared without user consent

How to reproduce:

  1. Write lots of data to localStorage in your iOS Cordova app
  2. Quit the app
  3. Make the device storage low by downloading music etc.
  4. Observe localStorage inside cordova app being cleared

Background

iOS5 introduced a feature where certain application data contents (non-permanent cached data) might get cleared if the device is running out of storage.

http://www.marco.org/2011/10/13/ios5-caches-cleaning

It seems the behavior also affects Cordova applications using localStorage. There are some reports in the internet about it and some of our internal testers reported the same issues on their iphones (on app built with Cordova 3.6).

http://forum.ionicframework.com/t/ios-localstorage-persistence/20004 http://forum.ionicframework.com/t/localstorage-is-it-cleared-after-app-restarts-periodically-in-ios/21819

Apparently the then-PhoneGap bug related to this was fixed long time ago, but it seems it reoccurs with new versions of iOS.

Related Android issues

There were also reports of bugs in Android 5 (Chrome 42) about last localStorage write being lost if the app is killed, but apparently the bug was quickly fixed https://code.google.com/p/chromium/issues/detail?id=479767

jakub-g avatar Oct 28 '15 09:10 jakub-g

Any word on a fix for this?

pmramos avatar Sep 15 '16 18:09 pmramos

@pmramos two options PouchDB or LokiJS which have tutorials linked off from http://gonehybrid.com/dont-assume-localstorage-will-always-work-in-your-hybrid-app/

The preferred method looks like LokiJS with an unofficial filesystem adapter to persist the in-memory data.

budda avatar Dec 19 '16 23:12 budda

So switching to Sqlite or pouch or lokijs is the only option? Should i assume this will be not be fixed from apple/cordova?

praveenkumarsinha avatar Dec 29 '17 03:12 praveenkumarsinha

I am using pouchDB and get this issue only when the app is first install and the user "hard" close the app..

Anyone has solution to this issue?

premtemp1 avatar Jan 18 '18 21:01 premtemp1

apple sucks... still no fix

i-me-mine avatar Feb 07 '18 06:02 i-me-mine

Any word on how it works with the new webview?

amritk avatar Aug 16 '18 01:08 amritk

@amritk We switched to WkWebView and experience still this problem.

Bessonov avatar Aug 31 '18 22:08 Bessonov

Does this apply also for IndexedDB??

Marko-Matijevic avatar Jun 26 '19 13:06 Marko-Matijevic

@amritk We switched to WkWebView and experience still this problem. I'm using "cordova-plugin-ionic-webview": "4.1.3", and the problem with suddenly cleared cache still exists. iOS phone have 120GB of free space. iOS 13.5.X

what could be the problem?

qliqdev avatar Jul 07 '20 08:07 qliqdev

For everyone with this issue, use something like this: https://github.com/TheCocoaProject/cordova-plugin-nativestorage

sandstrom avatar Jul 07 '20 15:07 sandstrom

Yea we ended up switching to

https://github.com/crypho/cordova-plugin-secure-storage

amritk avatar Jul 07 '20 18:07 amritk