Bug with functions

Link to the code: https://x.thunkable.com/projects/5d8788a7f050e26757c400bc/e3d8f0f4-5349-433d-8bb2-890b206a23ac/blocks

I’m having a problem where a function that retrieves GPS coordinates does not finish before the code following the function.

In the example program, currentLat variable is not updated by the getLatLon function before currentLat is assigned to the slot1 label.

Am I doing something wrong? I’ve distilled the code down to just the problem and the problem still occurs.

Changing the screen color is just part of the debugging. It seems that the screen color should end up being aqua, but rather it ends up being pink.

UPDATE, although not really a fix to the problem, I can put a pause right after the getLatLon call and it will assign the variables correctly. One second appears to be enough. Still, this needs to be fixed.

Thanks for the help!


It is normal for the system to do things without waiting for the GPS to return.
Firstly, getting a fix does take time: the GPS sensor needs to identify several satellites signals and correlate their position, this can takes 10 seconds or more. But also there are instances where you cannot get a GPS signal at all (you are in tunnel, in the basement of a building, in a parking garage, whatever).
If the system was to linger until it got a signal, it would be there stuck and your only way to regain control would be to terminate the app.
You don;t want that.

So the GPS is asynchronous. You issue a request to get a fix, and the GPS function will call you when it gets the data; meanwhile, your program can carry on doing whatever else you need, even if this whatever is you having it display “acquiring satellite; please stand by”.

What you do is something like this:


The system will no hang around with the GPS location request (for the record, this whole getGPS function could be a button click instead, as opposed to be a function called by a button click, or the result of a call from another function, including the start or load screen) and the program will keep running doing whatever other stuff it is programmed to do.
When the GPS sensor returns something, it will go back in the function at the “then do” level. It does not matter if your app is taking pictures and trying to geotag them at that moment, if there is an error, it will display a message that it could not get a GPS fix. Then, it will call the function “whattodowithfailedGPS” (which you could call anything you want, and do whatever you program it to do in the case you cannot get a GPS fix).
If you do NOT have an error, then you get the latitude, longitude and location data, and do whatever you want with them by passing them to your other function “whattodo” with that GPS data.
By definition, “whattodo” will never start UNTIL you get the GPS data.

1 Like

Thanks for the detailed response! [and the code!!]

It makes sense!