Dor Cohen
Dor Cohen

Reputation: 17100

Ionic localstorage doesn't persist

I'm using local storage for storing data in Ionic My problem is that the local storage doesn't persist as it does on Web.

On iOS the local storage is being deleted every few days and on Android it's worse and on some devices the local storage acts like a session storage and being wiped when the app closed.

This is my local storage service:

angular.module('app.core')
.factory('localstorage', ['$window', function ($window) {
  return {
    setObject: function (key, value) {
      $window.localStorage[key] = JSON.stringify(value);
    },
    getObject: function (key) {
      return JSON.parse($window.localStorage[key] || null);
    }
  }
}]);

I read this post: http://www.joshmorony.com/a-summary-of-local-storage-options-for-phonegap-applications/

Local storage gets a bit of a bad wrap, and is generally considered to be unreliable. I think the browsers local storage can be a viable option and it is reasonably stable and reliable, but, it is possible for the data to be wiped, which means for a lot of applications it’s not going to be a great option.

Is there any explanation to this?

Upvotes: 4

Views: 3302

Answers (1)

Dor Cohen
Dor Cohen

Reputation: 17100

After exploring the localstorage issue usage in ionic I can say one thing:

DO NOT USE IT if you need your data to be persisted!

I've decided to use PouchDB to keep my data persistent.

From Ashteya Biharisingh post:

The data that is saved in localStorage is supposed to be persisted even if you close the app or turn off your phone. In most cases this will work, but there are issues with the way iOS and Android manage localStorage on the devices.

iOS
On iOS 8 the localStorage is sometimes cleared when memory is low, as you can read here.

I recently experienced it myself on an Ionic app I was working on. I got a notification on my iPhone that it was running low on memory and when I opened up the app the localStorage was empty. I had another app on my device that was using localStorage as well, but that one was not cleared.

Android
I haven't tested this extensively on Android, but if you read this thread, you'll see that there are several reports that localStorage doesn't work as expected.

you can follow a pretty good guide here

Upvotes: 4

Related Questions