Running animation sprite while coroutine executes

Hi, I’m having a problem with coroutines while I do the initial load of my game, I have an animation running while loading my database for initial setup and I do this initial setup in a coroutine but the animation stop for a seconds can someone tell me what  I’m doing wrong?

This is my code

main.lua

local animation--global variable --after click a button this executes animation= display.newSprite( sheet, myApp.new\_animations.base\_loading ) animation.x,animation.y=display.contentCenterX,display.contentCenterY animation:play( ) if(isNew) and (myApp.userData.username==nil or myApp.userData.username=="") then local co=coroutine.create( function () composer.loadScene("src.registerView", false,{}) userOperations.resetDatabase(); userOperations.shuffleQuestions(); userOperations.shuffleChallenges(); userOperations.setUserInfo() display.remove( animation ) composer.gotoScene("src.registerView", {effect="crossFade", time = 100}) end) coroutine.resume(co) else composer.loadScene("src.welcomeView", false,{}) questionOperations.getNextCategory2() display.remove( animation ) composer.gotoScene("src.welcomeView", {effect="crossFade", time = 100}) end 

userOperations.lua

function userOperations.resetDatabase() DBHandler.restartDatabase() end function userOperations.shuffleQuestions( ) local questions=DBHandler.getQuestions(1)--difficulty 1 local pos=0 questions=shuffle(questions) DBHandler.saveQuestionsOrder(questions,pos) pos=#questions; questions=DBHandler.getQuestions(2) questions=shuffle(questions) DBHandler.saveQuestionsOrder(questions,pos) pos=pos + #questions; questions=DBHandler.getQuestions(3) questions=shuffle(questions) DBHandler.saveQuestionsOrder(questions,pos) end function userOperations.shuffleChallenges( ) local challenges=DBHandler.getChallenges(1)--difficulty 1 local pos=0 challenges=shuffle(challenges) for i=1,#challenges do challenges[i].position=i+pos end DBHandler.updateChallengeOrder(challenges) local pos=#challenges; challenges=DBHandler.getChallenges(2) challenges=shuffle(challenges) for i=1,#challenges do challenges[i].position=i+pos end DBHandler.updateChallengeOrder(challenges) local pos=pos + #challenges; challenges=DBHandler.getChallenges(3) challenges=shuffle(challenges) for i=1,#challenges do challenges[i].position=i+pos end DBHandler.updateChallengeOrder(challenges) end function userOperations.setUserInfo() local users={} users[1]=globals.userData DBHandler.addUsers(users) DBHandler.addUserCredit(globals.userData.id\_user,globals.livesData.register,0,0) DBHandler.addUserLives(globals.userData.id\_user,globals.coinsData.register,0,0) globals.userData.credit=DBHandler.getUserCredit(globals.userData.id\_user) globals.userData.lives=DBHandler.getUserLives(globals.userData.id\_user) globals.userData.progress=DBHandler.getUserProgres(globals.userData.id\_user) globals.userPreferences.created=true return true end local function shuffle(theTable) local theTable = theTable local n = #theTable local j local random = math.random for i=n-1, 1, -1 do j = random(i) theTable[j],theTable[i] = theTable[i],theTable[j] end return theTable end

DBHandler.lua

function dbHandler.restartDatabase() local res; local sql=[=[UPDATE category SET enabled=1; UPDATE question SET enabled=1; UPDATE challenge SET enabled=1; DELETE FROM question\_order; DELETE FROM answer; DELETE FROM user\_credit; DELETE FROM user\_live; DELETE FROM power\_log; DELETE FROM question\_log; DELETE FROM user;]=]; res=db:exec(sql); return res==sqlite3.OK; end function dbHandler.addUsers(users) local res; for i=1,#users do local q = [[INSERT INTO user VALUES (NULL, ']] .. users[i].username .. [[',']] .. users[i].avatar .. [[',]] .. users[i].score.. [[,]] .. users[i].level.. [[,]] ..os.time()..[[,]] .. users[i].current\_page.. [[);]] res=db:exec( q ) end return res==sqlite3.OK; end function dbHandler.getChallenges(dificulty) local data = {} for row in db:nrows('SELECT \* FROM challenge where dificulty='..dificulty) do data[#data+1] = { id\_challenge=row.id\_challenge, name = row.name, options= row.options, answer= row.answer, enabled= row.enabled, position=row.position, type=row.type } end return data; end function dbHandler.getQuestions(dificulty) local data = {} for row in db:nrows('SELECT \* FROM question WHERE dificulty='..dificulty) do data[#data+1] = { id\_question = row.id\_question, id\_category= row.id\_category, dificulty = row.dificulty, question=row.question, correct=row.correct, score=row.score, comment=row.comment, movie=row.movie, enabled=row.enabled, answer=row.answer, option\_a=row.option\_a, option\_b=row.option\_b, option\_c=row.option\_c } end return data; end function dbHandler.saveQuestionsOrder(questions,startPos) for i=1,#questions do local q = [[INSERT INTO question\_order VALUES (NULL,]] .. questions[i].id\_question .. [[,]] .. (startPos+i) ..[[,1);]] db:exec( q ) end end function dbHandler.updateChallengeOrder(questions) for i=1,#questions do local q = [[UPDATE challenge SET position=]] .. questions[i].position .. [[where id\_challenge=]] .. questions[i].id\_challenge ..[[;]] db:exec( q ) end end 

this is all what I’m doing while animation run on start, how can I solve the problem

  1. Maybe I missed it, but I see no yield calls and no where that you’re tracking time that has passed or work done to decide when to suspend.

  2. If the animation is stopping/hanging for ‘seconds’ you’re doing too much work per ‘session’ in your co-routines.

The value of the co-routine is that you do a small bite of work, ‘suspend the co-routine’ (by yielding), start again next frame, repeat.

If the work you do per session/slice is too long (takes near to or longer than a frame duration), you will block Corona and then you’ll see pauses and and hangs.

Take a look a this tutorial: https://coronalabs.com/blog/2015/02/10/tutorial-using-coroutines-in-corona/

Just to be clear (not sure if you’re thinking this or not), but coroutines are not separate threads and Corona is not multi-threaded.

Coroutines are suspendable functions that can be resumed later.  This gives you the ability to create a thread-like environment (sequence of execution) in a single-threaded environment.

The way you want to implement coroutine execution as not to impede Corona is as follows:

| Frame 1 | Frame 2 | Frame 3 | Frame 4 |--------------------|--------------------|--------------------|-------------------- ... etc. |AA CCC BB DD EE... |AA CCC BB DD EE... |AA CCC BB DD EE... |AA CCC BB DD EE... A - Small bit of Corona code including code to resume() the coroutine 'C' C - Coroutine work (do only a few milliseconds worth per slice/resume, then yield() ) B/D/E - Random other Corona work that starts up after the yield In total, per frame, the total execution time of A + B + C + D... MUST NOT add up to or be greater than (1000/FPS-target) millisecond. Ex: if your FPS target is 30. Total time spent per frame executing code should be MUCH LESS than 33.33 ms. I say MUCH LESS because, don't forget Corona has work to do too.

I

Thanks I will try to put yields between operations

Re-wrote and updated last post a bit, be sure to catch my corrections.

  1. Maybe I missed it, but I see no yield calls and no where that you’re tracking time that has passed or work done to decide when to suspend.

  2. If the animation is stopping/hanging for ‘seconds’ you’re doing too much work per ‘session’ in your co-routines.

The value of the co-routine is that you do a small bite of work, ‘suspend the co-routine’ (by yielding), start again next frame, repeat.

If the work you do per session/slice is too long (takes near to or longer than a frame duration), you will block Corona and then you’ll see pauses and and hangs.

Take a look a this tutorial: https://coronalabs.com/blog/2015/02/10/tutorial-using-coroutines-in-corona/

Just to be clear (not sure if you’re thinking this or not), but coroutines are not separate threads and Corona is not multi-threaded.

Coroutines are suspendable functions that can be resumed later.  This gives you the ability to create a thread-like environment (sequence of execution) in a single-threaded environment.

The way you want to implement coroutine execution as not to impede Corona is as follows:

| Frame 1 | Frame 2 | Frame 3 | Frame 4 |--------------------|--------------------|--------------------|-------------------- ... etc. |AA CCC BB DD EE... |AA CCC BB DD EE... |AA CCC BB DD EE... |AA CCC BB DD EE... A - Small bit of Corona code including code to resume() the coroutine 'C' C - Coroutine work (do only a few milliseconds worth per slice/resume, then yield() ) B/D/E - Random other Corona work that starts up after the yield In total, per frame, the total execution time of A + B + C + D... MUST NOT add up to or be greater than (1000/FPS-target) millisecond. Ex: if your FPS target is 30. Total time spent per frame executing code should be MUCH LESS than 33.33 ms. I say MUCH LESS because, don't forget Corona has work to do too.

I

Thanks I will try to put yields between operations

Re-wrote and updated last post a bit, be sure to catch my corrections.