Background
On the home screen of my app, I display a list of location-based content. So when the app loads, I get the users’ location and then pass that to the backend to retrieve the relevant content. I had been developing and testing in the browser as well as my Android device and had never seen an issue.
When I installed the app on my iPhone, I started seeing inconsistent results. This was after I had accepted the prompt to allow the app to access location, so I knew I was at least in the code that tries to get the location.
- Sometimes when the app would start, no data would be shown. I don’t want to show the user bad data if the location is not known.
- Sometimes the correct data was shown. Ah, that meant I was getting the location, passing it to the backend, and retrieving the correct info.
- I could start and stop the app quickly and get different results each time (that is, data or no data).
Troubleshooting
I added several alerts in various places including when I was receiving the location and when I was calling the backend to retrieve data. I noticed that sometimes I wouldn’t even be making the call to the backend.
I then added an alert inside app.component.ts under initializeApp() to check and make sure the platform was really ready.
This is when I noticed that my “platform is ready” alert was coming after the “get location” alert. Ugh! I was actually trying to get the location and retrieve the data before the platform was even ready! (Before the Geolocation plugin was ready.)
Resolution
For the main home screen I had in place, I did another check to make sure the platform was ready. If it was ready, then I would get the location and retrieve the backend data.
ngOnInit() {
this.platform.ready().then(() => {
this.getTheDataINeed(); // Gets location and retrieves data
});
}
I had never seen this before…the fact that the platform was not ready when my code was ready to init a component. It makes sense on why this is, but since I hadn’t experienced it on my iPhone, it was a head-scratcher.
It bit me this time (took up some time anyways) but it was a good learning experience.