Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ionic local storage not persisting on device

I have a problem. When I run my app on chrome data is persistent due to the local storage of te web browser. However, when running on the simulator, if I kill the app, data gets wiped. I have read that localstorage it is a non volatile storage primary used for storing simple data. A DB is not needed for my app, local storage is more than enough. Any reason why this is happening?

angular.module('ionic.utils', [])

.factory('$localstorage', ['$window', function($window) {
  return {
    set: function(key, value) {
      $window.localStorage[key] = value;
    },
    get: function(key) {
      return $window.localStorage[key];
    },
    setObject: function(key, value) {
      $window.localStorage[key] = JSON.stringify(value);
    },
    getObject: function(key) {
      return JSON.parse($window.localStorage[key] || '{}');
    },
    removeItem: function(key){
    $window.localStorage.removeItem(key);
    },
    removeByIndex: function (index) {
    $window.localStorage.removeItem($window.localStorage.key(index));
    },
    getByIndex: function (index) {
    return JSON.parse(($window.localStorage.key(index)));
    },
    clear: function (){
      $window.localStorage.clear();
    }

  }

}]);
like image 389
Joseph Ocasio Avatar asked Jun 02 '15 01:06

Joseph Ocasio


People also ask

Is ionic storage persistent?

This plugin will use UserDefaults on iOS and SharedPreferences on Android. These native classes are mostly used to define the default app state and are 100% persistent as the data is stored directly inside the OS settings.

What is the limit of local storage in ionic?

Ionic Local Storage uses the local storage system of browser for key/value pairs storing. The limit of Local Storage is only 5MB.

How do you use storage in ionic?

Setting up Ionic Storage With Ionic Storage we can save JSON objects and key/value pairs to different storage engines, unified through one interface. Ok in easy words this means, Storage will internally select which storage engine is available and select the best possible solution for us.

Is ionic secure storage free?

Try it free now.


1 Answers

i am working on ionic and used localstorage(simple html local storage function)

  window.localStorage['Invite-Code'] = $scope.passCode;

above code set the value in local storage.

 var val = window.localStorage['Invite-Code'] || false;

above code show the value of localstorage value, if exist or not then its return value or false. here 'Invite-Code' is key

like image 57
Anurag Pandey Avatar answered Sep 27 '22 02:09

Anurag Pandey