Weired concurrency issue with Storage


#1

Hi all,

although this post is somehow related to my other post (Hide splashscreen after storage is ready), I thought it would be better create an new topic since this is a more general question.

What I try to do is to read some values from the Local Storage Provider and store these values in an associative array. This is the code:

import { Injectable } from '@angular/core';
import { Storage } from '@ionic/storage';

@Injectable()
export class StorageProvider {

  private entries: { [key: string]: any} = {};

  constructor(public storage: Storage) 
  {
    console.log('Reading from storage...');
      this.storage.forEach((value, key, index) => {
        this.entries[key] = value;
        console.log('Read key '+key+' with value '+value+' from storage.');
      });
    console.log(this.entries);
    
    console.log('now showing keys...');

    for(var entryKey in this.entries)
    {
       console.log(entryKey+": ");
       console.log(this.entries[entryKey]);
    }

  }
}

Now comes the weired part: The last iteration never has any output. So I thought that the forEach over the storage object may be asynchronous and outputted the entries member right before the iteration. All entries are there. The console output is this:

0     480711   log      Reading from storage...
1     480728   log      [object Object]
2     480731   log      now showing keys...
3     482010   info     Ionic Storage driver:, asyncStorage
4     482139   log      Read key deviceId with value dZZA4gB4ec8:APA91bFGQJ66crrhdGa38SHoOy7pTUCKgIKqHSXuo_oY7_RvHGNAqSfpW6DOpqczvvgtUimEMFfXe6mNwxTqm6YriQJ2k3rMPw4ebrfaxNYxf9y3azej92lBs7TT_Wm6wHEMm4uy-2r5 from storage.
5     482235   log      Read key user with value [object Object] from storage.

So, I am totally confused about what happens here. If Storage.foreach was asynchronous (although no Promise or Observable returned) then I would understand the result (and the order of the output) BUT only if entries was empty (which it is not). Well, if it is not empty then why does the iteration not work? I tested the same code in a seperate typescript and it works.

Any ideas on this?

Cheers,
Thomas


#2

OK, abandoned Storage and switched over to PouchDB. Works…