display.NewText shows only after database insert is done?

Hey all, when using storyboard scenes, I add some text and a button in the following section that calls a function to do a bunch of database inserts to create new teams.  The problem is that text2 does not show up until AFTER the database activity is complete, even though I call it before the database activity.  See the excerpt from the function below.  What am I doing wrong?

function scene:createScene( event )

    screenGroup = self.view

    

        local path = system.pathForFile(“Data.db”, system.DocumentsDirectory)

        db = sqlite3.open( path )   

    text2 = display.newText( “Creating Teams.”, centerX, 150, native.systemFontBold, 40 )

    text2:setFillColor( 1 )

    text2.isVisible=false

    screenGroup:insert( text2 )

    local function handleButtonEvent( event )   

    local myButtons = event.target

    local ButtonName = myButtons.id

    if ( “ended” == event.phase ) then

        if (ButtonName == “CreateTeamsButton”) then

            CreateTeams()

        end 

    end

end

local CreateTeamsButton = widget.newButton

{

    id=“CreateTeamsButton”,

    width = 369,

    height = 87,

    defaultFile = “CreateTeamsButton.png”,

    overFile = “CreateTeamsButton-Over.png”,

     onEvent = handleButtonEvent,

    x=870,

    y=720,

}

screenGroup:insert( NewGameButton )

end


function CreateTeams()

    text2.isVisible=true

    local text3 = display.newText( “Please wait…”, centerX, 200, native.systemFontBold, 40 )

    text3:setFillColor( 1 )

    screenGroup:insert( text3 )

db:exec(“insert into teams …[rest of the query is here]”)

…[there are about 30 queries that run here that take awhile]

text2.text="DONE!’

end


Why does text 2 and text3 show up only AFTER the database queries finish and not before?

Thanks!

Anyone??? I’m stumped here… Why does the text show AFTER the database queries are done?  I attempt to call it before I execute the db code… What gives?!?

Try to put a tiny delay before inserting to give the display time to update.

[lua]

timer.performWithDelay(50, function() db:exec(“insert into teams …[rest of the query is here]”) end)

[/lua]

So, if I have multiple queries, should I add each one to a delay or give them all the same delay?

Just a tiny delay after showing your “Please wait” text. Then do all the inserts at the same time is fine.

Can put all inserts into separate function inside createTeams function for example:

[lua]

function CreateTeams()

 

    local function insertQueries()

        db:exec(“insert into teams …[rest of the query is here]”)

        …[there are about 30 queries that run here that take awhile]

        text2.text="DONE!’

    end

 

    text2.isVisible=true

 

    local text3 = display.newText( “Please wait…”, centerX, 200, native.systemFontBold, 40 )

    text3:setFillColor( 1 )

    screenGroup:insert( text3 )

    timer.performWithDelay(10, insertQueries)

 

 

 

end

[/lua]

Wow! Thanks!  If I want to to update the text with the progress - something like “Creating Teams - Team 1” and then updating the text to say “Creating Teams - Team 2” etc, during the query process, how would I make sure it shows up?

You would have to do it step by step then, with delays in-between.

Anyone??? I’m stumped here… Why does the text show AFTER the database queries are done?  I attempt to call it before I execute the db code… What gives?!?

Try to put a tiny delay before inserting to give the display time to update.

[lua]

timer.performWithDelay(50, function() db:exec(“insert into teams …[rest of the query is here]”) end)

[/lua]

So, if I have multiple queries, should I add each one to a delay or give them all the same delay?

Just a tiny delay after showing your “Please wait” text. Then do all the inserts at the same time is fine.

Can put all inserts into separate function inside createTeams function for example:

[lua]

function CreateTeams()

 

    local function insertQueries()

        db:exec(“insert into teams …[rest of the query is here]”)

        …[there are about 30 queries that run here that take awhile]

        text2.text="DONE!’

    end

 

    text2.isVisible=true

 

    local text3 = display.newText( “Please wait…”, centerX, 200, native.systemFontBold, 40 )

    text3:setFillColor( 1 )

    screenGroup:insert( text3 )

    timer.performWithDelay(10, insertQueries)

 

 

 

end

[/lua]

Wow! Thanks!  If I want to to update the text with the progress - something like “Creating Teams - Team 1” and then updating the text to say “Creating Teams - Team 2” etc, during the query process, how would I make sure it shows up?

You would have to do it step by step then, with delays in-between.