Why platform.ready() is not firing ? (And should I use Platform.prepareReady() to fix it ?)


#1

Hello,

I had much troubles using native plugins on Android (tested on fresh installs of 6.1.1 on Nexus 5 and 8.0.0 on Nexus 5x) because platform.ready() would not fire at any time. And using them outside of the callback would cause them to randomly work.

browsing forums I found 2 known solutions for this issue :

  • reinstall dependencies, plugins and platform
  • shorten app startup length

First I started a test project with only the plugin I needed (phonegap-plugin-push) and noticed that platform.ready() was firing just as supposed.

Then, back to my real project, I tried to uninstall all cordova plugins, disabling all none needed JS, deleting any external causes I could (for example, one of the font was still downloaded from the internet instead of being a local ressource), downgrading some npm dependencies to match the test project. Of course there were still a lot more of dependencies and at this point but I could not really do anything about it.

Still no platform.ready()

The problem occured even with --prod, which was fixing both cordova message “deviceready has not fired after 5 seconds” and ionic message “Ionic Native: deviceready did not fire within 5000ms. This can happen when plugins are in an inconsistent state. Try removing plugins from plugins/ and reinstalling them.”

Then I dug the Platform module and thru my IDE I found member functions that was not documented on (based on https://ionicframework.com/docs/api/platform/Platform/)

The one which did the trick was Platform.prepareReady()
Even without --prod, with cordova and ionic complaining that startup higher than 5 second, my console.log() wrapped in the platform ready would finally display.

So what is the deal with that function please ? It is not documented.
Even if that makes my plugin work, I’m kinda afraid that maybe I fire the ready manually even if the device is not really ready yet and I could cause bugs at the release time, on slower device for example.


#2

I finally found an explanation. I feel a bit stupid now but I will answer my own question. Hope it will help.

At first, prepareReady() is supposed to be called by Ionic. Basically, it sets a listener for some DOMready event or if you’re on a device, it calls a much specific Cordova method which listens for a device ready event. Then the callback will call platform.ready() where you’re implement your own callback.
I supposed it is not documented on the website (it is on the sources actually) because we shouldn’t call it by ourselves.

So the real question is : why no ready event was ever prepared ?

Because I was porting an already existing Angular 2 application to ionic and I made a big mistake :

In app.module.ts when replacing the default entry component with my app’s one, I accidentally modified this line :

bootstrap: [IonicApp]

to

bootstrap: [MyActualEntryComponent]

Doing that, I lost some Ionic behaviours.

So if you encounter my problem, be sure you keep the bootstraper pointing to IonicApp and set your entry component using this line into imports :

IonicModule.forRoot(MyActualEntryComponent)