Issue with "applicationResume" - Function does not get executed

So I am trying to make my app check if it should load data after Resuming the app. On resume it has to execute  a callback to App42.

When It resumes it reaches the point where it prints : (“I’m in, yes!!”). If I suspend the app…the CallBack continues and I get the data I need. But it is too late. I want the data on Resuming.

It is weird, sometimes (rarely) it works and I get the data on Resume. It looks like when trying to execute the App42 API ,it just blocks the execution.

I have been reading about coroutines and yielding but I am totally lost. 

What is happening? How can I fix it?

    elseif (event.type == "applicationResume" )then                              local function updatedgamesFetch(event)                    if event ~= 404 and event then                        local gamesupdatedtable = event                        if  table.indexOf(gamesupdatedtable, docId) then                             print("load data from app42")                        end                    end                 end                 utility.checkupdatedgames(updatedgamesFetch,localplayer)         print("Application resumed from suspension")                   end end

Checkupdatedgames looks like this

function M.checkupdatedgames(returnHandler,localplayer)             if ( returnHandler and type(returnHandler) == "function" ) then               M.callback = returnHandler            else               error( "No callback function listed" )            end             local q1 = queryBuilder:build("localplayer",localplayer,Operator.NOT\_EQUALS)             local q2 = queryBuilder:build("uid1",localplayer,Operator.EQUALS)             local q3 = queryBuilder:build("uid2",localplayer,Operator.EQUALS)             local queryI = queryBuilder:compoundOperator(q2,Operator.OR,q3)             local query = queryBuilder:compoundOperator(queryI,Operator.AND,q1)             print("I'm in, yes!!")             local App42CallBack3= {}             local gamesupdatedtable ={}             storageService2:findDocumentsByQuery(dbName, collectionName,query,App42CallBack3)             function App42CallBack3:onSuccess(object)                     print("I'm inside onSuccess")                     for i=1,table.getn(object:getJsonDocList()) do                           print("DocId is "..object:getJsonDocList()[i]:getDocId())                         table.insert(gamesupdatedtable,object:getJsonDocList()[i]:getDocId())                     end                     M.callback(gamesupdatedtable)             end               function App42CallBack3:onException(exception)                 print("I'm inside the Exception")                 print("Message is : "..exception:getMessage())                   print("App Error code is : "..exception:getAppErrorCode())                   print("Http Error code is "..exception:getHttpErrorCode())                   print("Detail is : "..exception:getDetails())                 M.callback(exception:getHttpErrorCode())             end end

Is storageService2:findDocumentsByQuery doing a network.request? Network requests are asynchronous, and the code moves on from that call without waiting for operation to complete, as the callback will handle the control when it comes back.

Therefore, you will have print(“Application resumed from suspension”) before the callback has occurred. 

One possible approach could be to show a waiting indicator when you resume

native.setActivityIndicator( true )

And then remove it during the callback

native.setActivityIndicator( false )

This way the App shows a waiting indicator while it decides and loads the data needed. 

Hope this helps.

Hi

That is what I was thinking since it works for other callbacks from app42 that I am using. But what I found  really weird is that the callback gets completed once I suspend again the application.

I tested your solution, and it shows the waiting indicator really fast and prints (“I’m in, yes”) …and that is it.

For Instance I open the app, then I suspend it. Then I resume and I see : “I’m in, yes!!” . Then I wait for a while (more than 10 seconds…) then I suspend the app again and I see the rest of the callback executing : “I’m inside onSuccess”, “load data from app42”.

The same happens if I run the callback from any other part of the code besides “applicationResume”. Looks like something gets blocked.

Since storageService2:findDocumentsByQuery  is a network request, maybe is local function updatedgamesFetch(event) that gets blocked…I dont know but it is driving me crazy

I also tried to remove the code from the module and run it directly with same results.

If you resume the App and wait, how long does it take for the callback to complete? (i.e. without suspending the App and just waiting)

It never completes. Only when I make another action like change scene or re-suspend. Then the callback completes.

What I find interesting is that looking at the log, the result from the callback is instant but I only see it after doing the action. Is like the result of the callback is there…just not showing :

Apr  5 15:36:32 Stark.fritz.box regiowars[6735]: I’m in! yes!
Apr  5 15:36:33 Stark.fritz.box securityd[6686]:  SecTaskCopyAccessGroups No keychain access group specified whilst running in simulator, falling back to default set
Apr  5 15:36:33 Stark.fritz.box regiowars[6735]: I’m in! succes!
Apr  5 15:36:33 Stark.fritz.box regiowars[6735]: DocId is XXXXXXXXXXXX
Apr  5 15:36:33 Stark.fritz.box regiowars[6735]: show dices on menu

In this example I see the I’m in!yes!  I wait for 2 minutes until 15:38 and the change scene and I see the rest of the callback. But the log states 15:36.

This might be a long shot but try removing “M.callback(gamesupdatedtable)” and see if the onSuccess print statements get printed? 

Same thing :frowning:

It is solved. Actually the only problem is with the printing of the log messages. The log is correct on the timing…the callback is getting executed is just the printing of the values that goes with delay :slight_smile:

Is storageService2:findDocumentsByQuery doing a network.request? Network requests are asynchronous, and the code moves on from that call without waiting for operation to complete, as the callback will handle the control when it comes back.

Therefore, you will have print(“Application resumed from suspension”) before the callback has occurred. 

One possible approach could be to show a waiting indicator when you resume

native.setActivityIndicator( true )

And then remove it during the callback

native.setActivityIndicator( false )

This way the App shows a waiting indicator while it decides and loads the data needed. 

Hope this helps.

Hi

That is what I was thinking since it works for other callbacks from app42 that I am using. But what I found  really weird is that the callback gets completed once I suspend again the application.

I tested your solution, and it shows the waiting indicator really fast and prints (“I’m in, yes”) …and that is it.

For Instance I open the app, then I suspend it. Then I resume and I see : “I’m in, yes!!” . Then I wait for a while (more than 10 seconds…) then I suspend the app again and I see the rest of the callback executing : “I’m inside onSuccess”, “load data from app42”.

The same happens if I run the callback from any other part of the code besides “applicationResume”. Looks like something gets blocked.

Since storageService2:findDocumentsByQuery  is a network request, maybe is local function updatedgamesFetch(event) that gets blocked…I dont know but it is driving me crazy

I also tried to remove the code from the module and run it directly with same results.

If you resume the App and wait, how long does it take for the callback to complete? (i.e. without suspending the App and just waiting)

It never completes. Only when I make another action like change scene or re-suspend. Then the callback completes.

What I find interesting is that looking at the log, the result from the callback is instant but I only see it after doing the action. Is like the result of the callback is there…just not showing :

Apr  5 15:36:32 Stark.fritz.box regiowars[6735]: I’m in! yes!
Apr  5 15:36:33 Stark.fritz.box securityd[6686]:  SecTaskCopyAccessGroups No keychain access group specified whilst running in simulator, falling back to default set
Apr  5 15:36:33 Stark.fritz.box regiowars[6735]: I’m in! succes!
Apr  5 15:36:33 Stark.fritz.box regiowars[6735]: DocId is XXXXXXXXXXXX
Apr  5 15:36:33 Stark.fritz.box regiowars[6735]: show dices on menu

In this example I see the I’m in!yes!  I wait for 2 minutes until 15:38 and the change scene and I see the rest of the callback. But the log states 15:36.

This might be a long shot but try removing “M.callback(gamesupdatedtable)” and see if the onSuccess print statements get printed? 

Same thing :frowning:

It is solved. Actually the only problem is with the printing of the log messages. The log is correct on the timing…the callback is getting executed is just the printing of the values that goes with delay :slight_smile: