Ionic3-Cordova-UWP : Ionic Storage doesn’t work when app is running


#1

Hi everyone,
I’m new on Ionic Forum so sorry if I misunderstood the way to do a post.
I have a problem with Ionic storage in my Ionic App for Windows (UWP app). I already tested and deployed my app for Android without any trouble.
Now I would like to generate an UWP app.

The app I’m developing is a workshop’s dashboard. The indicators change every day and I need to save them from one day to another. To do this I usethe storage module of Ionic.
When launching the command $ionic serve , the app responds exactly as it should in the web browser (Firefox). From one launch to another my data are still there.
When I use $ionic cordova run windows , the installation and launch steps of my app work fine. Every function of my app runs fine, but the call to storage (get() and set()) doesn’t work : when I close, and then reopen it as an autonomous application, I loose all data every time. It is the same when I run the app from Visual Studio.

Moreover, I write some console.log() in the result of the promise and none of them is written in the Javascript console. It is like Windows does not recognize the command.

I tried with Visual Studio 2015 Update 3 and Visual Studio 2017, the result is identical. I followed the recommendation of Ionic Doc and add the target platform windows10 in the config.xml.

Does anyone have an idea of what I’m doing wrong ? Is there an incompatibility between Windows and Ionic ?

Thanks for any help.

My Config :

Windows 10 64-bits
Ionic CLI: 3.19.1
Cordova CLI : 8.0.0
Node : v6.11.2
Visual Studio Community 2017 (15.5.27130.2036)

Extract of my code maPage.ts :

import { Storage } from '@ionic/storage';

export class maPage {
   constructor(public storage: Storage) {
	    this.downloadData();
	}

   ionViewWillLeave()
   {
     this.saveData();
   }

  TabData = {
	         id: 0,
	         label: '',
	         Tab1: [],
	         Tab2: [],
	         attri1 : 0,
	         attri2: false
	       }
  Param = {
	    Objet1: {NbObj1: 7},
	    Objet2: {NbObj2: 5}
	  }

 saveData()
	{   
	    var Data: object;
	    Data = 
	    {
	      TData: this.TabData,
	      TParam: this.Param
	    }
	    this.storage.set('Data', Data).then(_=> {
	      console.log('Backup done!');    
	    }, error => {
	      console.log('erreur : ', JSON.stringify(error))
	    });
	}

  downloadData()
	{
	   this.storage.ready().then(()=>{
    	         console.log('storage ready');
                 this.storage.get('Data').then((val) => {
	                this.TabData = val.TData;
	                this.Param = val.TParam;
	                console.log('Récupération terminée !');
	        }).catch(err => {  
	             console.log('La variable Data est vide ou n\'existe pas!');
	            console.log('err : ', JSON.stringify(err));
	  }).catch(err => {
               console.log('storage not ready');
               console.log('err : ', JSON.stringify(err));
         });
  }
}

#2

HI , in windows you should use SQLite :
ionic cordova plugin add cordova-sqlite-storage

you can use localStorage if you don’t want use SQLite ,
localStorage.setItem("test",var); localStorage.getItem('test')


#3

Hi @guadyass,

Thanks for your answer. What do you want to do by ‘using SQLite’ ? Do you mean not using the storage module of Ionic or just install the cordova-sqlite-storage plugin ?
If the second, I forgot to mention that I already installed this plugin, which occures, by the way, some difficulties because of SQLite3 (I can’t generate anycpu, I have to generate x86).

Thank you in adance for your response.


#4

I mean if you have a problem with Storage pluging you can use localstorage
(Local Storage or Storage or NativeStorage?)

or you can use SQLite and use the classic command like “create table …” (https://ionicframework.com/docs/native/sqlite/)


#5

Finally I used localstorage and it works. Thank you.

However, I don’t understand why native storage from Ionic doesn’t work on windows.


#6

I understood that the problem with localstorage is that it may get automatically deleted by the mobile OS, if it decides in needs more space.

To the point… Is this still not solved? I have exactly the same problem. When I build UWP app, set and get doesn’t seem to do anything.

Also after spending several days, i find the whole Ionic storage solution quite wonky because I have the following problems:

  • LocalStorage may get deleted anytime => useless
  • NativeStorage does not work for me at all (at least not with UWP and also not in browser - which is documented and makes sense)
  • Ionic Storage cannot be built with anycpu for UWP because of SQLite making it not UWP at all…

It seems to me I have one last chance trying Ionic Storage without SQLite. Still, this part of otherwise great Ionic + Cordova feels a bit “unfinished”.


#7

I confirm that Ionic Storage without SQLite works fine. It is enough just to uninstall sqlite plugin and Ionic Storage automatically switches to something else that works. This is however the only combination that has worked for me.