Local storage value does not record on several devices


#1

Hello,

I’m trying to understand what can be reason. I’m using local storage persistent value for user ID in Ionic2 app:

  set() {
    var val = this.user_id;
    this.storage.set('myData', val);
  }

I have 6 Android device. Value from local storage was user ID taken from val with all listed devices, except LG which not was tested before problem:

 this.storage.get('myData').then((val) => {     
      this.user = val;     
 });

All this time it was:

1. Motorola motoe4           |       val = 129299383782734664 
2. Motorola motoe4plus       |       val = 129299383782734664 
3. Asus                      |       val = 129299383782734664 
4. Samsung                   |       val = 129299383782734664
5. Samsung (old model)       |       val = 129299383782734664 

Now I got second result:

1. Motorola motoe4           |       val = 129299383782734664 
2. Motorola motoe4plus       |       val = 129299383782734664 
3. Asus                      |       val = 129299383782734664 
4. Samsung                   |       val = null
5. Samsung (old model)       |       val = 129299383782734664 
6. LG                        |       val = null   

Seems like local storage does not works on 4. Samsung and 6. LG, value is null. Not sure if it can be reason, but Samsung display was replaced, this coincides with the time when result was proper, and then with null after display repairing, but what then with LG, I lent this phone for testing, also does not stores value in local storage, equal to null. Maybe it is something wrong with SQLite on this devices

Advice would be helpful


#2

This returns a Promise. You are ignoring it. You therefore have no way of knowing when the write has completed. Specifically, if you then attempt to read that same value, there is no guarantee whether the new or old value is present. This is why I recommend avoiding storage for in-app communication, and only using it to communicate across app restarts. In other words, if the only time you ever read from storage is at app startup, you don’t have to worry about race conditions here.


#3

@rapropos Hello,

thank you for feedback

Even if I will change method to get persistent value, anyway I use local storage with restart conditions too, where value is not user ID. I’ve checked, In this case storage also does not works on Samsung and LG, which set and get data produced not at the same time sequentially.

Storage does not stores value on this devices, val is null with set on load, and then get after certain period of time, set with load, and get with click from some page for example, recording should be complete, if I understand you correct, about race conditions, when subsequent get is ahead of the set record


#4

I don’t think you do. The only way to ensure that whatever you are doing happens after the set has finished is to wait on the Promise it returns. Period. This has nothing whatsoever to do with devices. It is actively counterproductive for you to try these things on various devices and consider blaming the device. I describe this in some more detail here.


#5

@rapropos Thank you

In my particular case problem was by lack of SQLite plugin in project. Of course to set and get I should return with Promise get<T>(key: string): Promise<T> sequentially:

    return this.setData('myData', this.user).then(
      () => console.log('set()', data)
    ).then(
      () => this.getData(key)
    ).then(
      value => console.log('get()', value)
    )