Executing Functions in a Specific Order

I know Corona executes functions asynchronously, but I have an instance where I want specific functions to fire off first and so on. 

I have a button.  Tap the button changes the button state to active and executes some sqlite stuff.  Here’s an overly-simplified example of what I tried:

local update = function()

   db:exec(“more SQL that updates a table”)

end

local save = function()

   db:exec(“some SQL that daves data to a table”)

   update()

end

local activeButton = function()

    myButton:removeSelf()

    local activeButton = display.newImage( “location.png”, 50, 50 )

    save()

end

local myButton = display.newImage (“location.png”, 50, 50)

    myButton:addEventListener( “tap”, activeButton )

To me it looks like Corona will change out the button THEN execute the save function THEN execute the update function.  

What actually happens is Corona execute the db functions THEN changes the button state. 

The SQL queries are complex enough that there is a noticeable delay between the button tap and the changing of the button state. 

All I really want is for Corona to change the button state immediately then execute the SQL.  

Any tips or advice will be greatly appreciated. 

Hi there,

The code does actually execute in the order you first described.  First the code to change out the button is executed, then the save function is executed, and then the update function is executed.  You can verify this by inserting some print statements in the code.

However, it’s also true that you won’t see the change in the button until after the SQL functions finish executing.  That’s because Corona renders the screen only once per frame after all of the code has executed.  Your SQL functions apparently take a bit of time to execute, so that’s why you’re experiencing a delay between the button press and the visual change to the button.  Another way to say it is that, when your SQL functions are executing, your frame rate is dropping below 30fps, hence the visual lag.

One quick way to get the button to change immediately on the screen is to replace save() with timer.performWithDelay(1, save).  This will force Corona to wait to execute the SQL functions until the next frame, by which point your button will have already been updated on screen.

You might also consider if there are ways to optimize your SQL queries to get them to run faster, so they execute comfortably within a single frame and therefore don’t cause any lag.

Does that make sense?

  • Andrew

That was very helpful.  Thank you.

:smiley:

Hi there,

The code does actually execute in the order you first described.  First the code to change out the button is executed, then the save function is executed, and then the update function is executed.  You can verify this by inserting some print statements in the code.

However, it’s also true that you won’t see the change in the button until after the SQL functions finish executing.  That’s because Corona renders the screen only once per frame after all of the code has executed.  Your SQL functions apparently take a bit of time to execute, so that’s why you’re experiencing a delay between the button press and the visual change to the button.  Another way to say it is that, when your SQL functions are executing, your frame rate is dropping below 30fps, hence the visual lag.

One quick way to get the button to change immediately on the screen is to replace save() with timer.performWithDelay(1, save).  This will force Corona to wait to execute the SQL functions until the next frame, by which point your button will have already been updated on screen.

You might also consider if there are ways to optimize your SQL queries to get them to run faster, so they execute comfortably within a single frame and therefore don’t cause any lag.

Does that make sense?

  • Andrew

That was very helpful.  Thank you.

:smiley: