storage.ready Promise not really ready in cordova with sqlite plugin
See original GitHub issueHi, I’m using
this.storage.ready().then(() => {
console.log('ready');
});
to start accessing the storage. In the browser this works perfect. On android with the cordova-sqlite-storage
I see that ready
is logged to the console and then the debugging logs of the sqlite plugin start to appear. DB open
and DB opened
.
Which means that if I use storage.get
in this tiny timeframe I don’t receive the real values. I found out that sometimes it works, sometimes it doesn’t, probably a performance question of the device.
After browsing through the code I see that ionic storage is returning the localForage instance.
A bit more browsing in the localForage docs shows that localForage also has a ready Promise that is not accounted for in the ionic storage. https://localforage.github.io/localForage/#driver-api-ready
The workaround I use to be sure the sqlite db is loaded. I am just using the returend localForage object and listen again to the ready promise. Like this the everything is initialized and I can safely access my data afterwards.
this.storage.ready().then((localForage) => {
console.log('ready');
localForage.ready(() => {
console.log('really ready');
})
});
Ionic info
Ionic:
ionic (Ionic CLI) : 4.10.3 (/usr/local/lib/node_modules/ionic)
Ionic Framework : @ionic/angular 4.0.2
@angular-devkit/build-angular : 0.12.4
@angular-devkit/schematics : 7.2.4
@angular/cli : 7.2.4
@ionic/angular-toolkit : 1.4.0
Cordova:
cordova (Cordova CLI) : 8.1.2 (cordova-lib@8.1.1)
Cordova Platforms : android 7.1.4
Cordova Plugins : cordova-plugin-ionic-keyboard 2.1.3, cordova-plugin-ionic-webview 4.0.0, (and 10 other plugins)
System:
NodeJS : v8.10.0 (/usr/bin/node)
npm : 6.9.0
OS : Linux 4.15
Issue Analytics
- State:
- Created 5 years ago
- Reactions:2
- Comments:5 (2 by maintainers)
Is there still a ready event in v3? We have a fairly specific use case where we need to read data from storage the moment ngrx effects in an ionic/angular app are initialized. The problem we have is there now only seems to be a storage.create() method, there no longer appears to be a storage.ready() method that we can block things on until storage is in fact ready to be used.
@jrista you don’t need a ready() anymore. The old library had a hard coded
defineDriver
for theCordovaSQLitePlugin
which was the only promiseready()
had to wait on anyways.Instead, explicitly wait on any
defineDriver
and after callingcreate
the DB is ready to use.Not sure if you’re using angular but take look at the
init()
method here:https://github.com/ionic-team/ionic-storage#with-angular