question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

storage.ready Promise not really ready in cordova with sqlite plugin

See original GitHub issue

Hi, 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.

https://github.com/ionic-team/ionic-storage/blob/2ea8583d774a96c3c150e944c8fe925d0cf69f3d/src/storage.ts#L134

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:closed
  • Created 5 years ago
  • Reactions:2
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
jristacommented, May 10, 2021

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.

0reactions
mlynchcommented, Jun 14, 2021

@jrista you don’t need a ready() anymore. The old library had a hard coded defineDriver for the CordovaSQLitePlugin which was the only promise ready() had to wait on anyways.

Instead, explicitly wait on any defineDriver and after calling create 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

Read more comments on GitHub >

github_iconTop Results From Across the Web

cordova sqlite - problem with selecting from database
opening database seems to be working (based on cordova documentation and sample): var DB; var onDeviceReady = new Promise(function (resolve, ...
Read more >
cordova-sqlite-storage - npm
Native interface to SQLite for PhoneGap / Cordova - cordova-sqlite-storage plugin version. Latest version: 6.1.0, last published: 14 hours ...
Read more >
How to make SQLite in ionic-native use cordova-sqlite-ext?
In my app, I have cordova-sqlite-ext installed (cordova-sqlite-storage is NOT installed). Apparently, SQLite is unable to find the sqlite plugin ...
Read more >
Building an SQLite Ionic App with Capacitor - Devdactic
At the time writing the plugin did not have a web implementation, ... to the database stead and not perform any queries before...
Read more >
Sqlite and Cordova - Getting started & what I wish I knew up front
cd path/to/project cordova plugin add https://github.com/litehelpers/Cordova-sqlite-storage. Now you're ready to create a database!
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found