Hi guys, when I build my iOS app and run it in iOS Simulator or directly in my iPhone, ONLY the first bootstrap I can’t write/read any objects inside my localstorage. If I force stopping the app and re-run it, all works fine…
I use localStorageService.
In .run method inside $ionicPlatformReady, I check if localstorage object exists and I create new one (empty array) if it return null or undefined:
if (localStorageService.get('territories') === null || typeof localStorageService.get('territories') == 'undefined') {
localStorageService.set('territories',[]);
}
then, in my factory:
.factory('Territories', function(localStorageService) {
var territories = localStorageService.get("territories");
return {
all: function() {
return territories;
},
set: function(territory){
territories.push(territory);
return localStorageService.set("territories",territories);
},
remove: function(territory) {
territories.splice(territories.indexOf(territory), 1);
return localStorageService.set("territories",territories);
},
update: function(updatedTerritory){
for (var i = 0; i < territories.length; i++) {
if (territories[i].id === updatedTerritory.id) {
territories[i].name = updatedTerritory.name;
localStorageService.set("territories", territories);
}
}
return null;
},
get: function(terrId) {
for (var i = 0; i < territories.length; i++) {
if (territories[i].id === terrId) {
return territories[i];
}
}
return null;
}
};
})
and in my controller:
.controller('notesCtrl', function($scope, $ionicPopup, Territories, Visits, Owners, Addresses, $ionicListDelegate, $cordovaDialogs, $ionicPlatform) {
$scope.territories = Territories.all();
$scope.addTerritory = function(){
$ionicPlatform.ready(function() {$cordovaDialogs.prompt('Inserisci il nome e il numero di territorio', 'Nuovo territorio', ['Annulla','Crea'], '')
.then(function(result) {
var input = result.input1;
var btnIndex = result.buttonIndex;
if (btnIndex == 0){
return;
}else{
$scope.$watch('territories', function(){
var territory = {
"id": Math.random().toString(36).substr(2, 9),
"name":input
};
Territories.set(territory);
$scope.territories = Territories.all();
})
}
});
});
};
All works fine in browsers, Safari included, and in Ionic View. But, as I mentioned above, in the simulator it works only from the second bootastrap. It seems that the app don’t loads LocalStorage at the first start.
Help me please :’-(