suspend/exit with network request problem

hello

i have a problem while suspending or exiting an app.
when i suspend/exit with the home button, i want my app to send a network request to a server to checkout of a game, so i made this code:

[code]local onSystem = function( event )

if event.type == “applicationSuspend” or event.type == “applicationExit” then

if _G.actualSeat then

networkrequests.onExit()

end
–timer.performWithDelay(1,function() end)
os.exit()
end

end

Runtime:addEventListener( “system”, onSystem )[/code]

but sometimes it runs when i click on home button, other times it runs when i resume the application, what can i do, any workaround? i really need to checkout when suspend/exit.

using build 878 with ipad3

thks,
Edou Suilen [import]uid: 92107 topic_id: 30052 reply_id: 330052[/import]

What you are trying to do is unreliable. A network request is an asynchronous operation and there is no guarantee that it will before your app exits. Odds are, it won’t get sent. Especially with that [lua]os.exit()[/lua] function call in your code, because that will terminate your program along with any pending network requests.

I recommend that you remove the [lua]os.exit()[/lua] from your code. That’s a really bad way to exit your program and it might get your app rejected by an app reviewer because a sudden exit might be interpreted as a crash. If you want to close your app gracefully, then you should call [lua]native.requestExit()[/lua] instead, which will close the window but will leave the application running in the background which is needed to carry out the network request which is executing on another thread. [import]uid: 32256 topic_id: 30052 reply_id: 120340[/import]

thks for the fast response,

i removed the os.exit, but even then it doesnt work all the time, is there no workaround possible? [import]uid: 92107 topic_id: 30052 reply_id: 120467[/import]

Oh wait… I just noticed that you are doing this on iOS. What I recommended would work on Android but likely not on iOS. This is because an async network request depends on the app’s message event queue which becomes inactive when you leave the app.

In this case, there is no way to do this via an app suspend or exit event. Instead, I recommend that you store the data that you want sent to your sever to file and later send that data when your app resumes/starts. This is how Corona handles analytics data. It is the only reliable way of handling it. [import]uid: 32256 topic_id: 30052 reply_id: 120520[/import]

What you are trying to do is unreliable. A network request is an asynchronous operation and there is no guarantee that it will before your app exits. Odds are, it won’t get sent. Especially with that [lua]os.exit()[/lua] function call in your code, because that will terminate your program along with any pending network requests.

I recommend that you remove the [lua]os.exit()[/lua] from your code. That’s a really bad way to exit your program and it might get your app rejected by an app reviewer because a sudden exit might be interpreted as a crash. If you want to close your app gracefully, then you should call [lua]native.requestExit()[/lua] instead, which will close the window but will leave the application running in the background which is needed to carry out the network request which is executing on another thread. [import]uid: 32256 topic_id: 30052 reply_id: 120340[/import]

thks for the fast response,

i removed the os.exit, but even then it doesnt work all the time, is there no workaround possible? [import]uid: 92107 topic_id: 30052 reply_id: 120467[/import]

Oh wait… I just noticed that you are doing this on iOS. What I recommended would work on Android but likely not on iOS. This is because an async network request depends on the app’s message event queue which becomes inactive when you leave the app.

In this case, there is no way to do this via an app suspend or exit event. Instead, I recommend that you store the data that you want sent to your sever to file and later send that data when your app resumes/starts. This is how Corona handles analytics data. It is the only reliable way of handling it. [import]uid: 32256 topic_id: 30052 reply_id: 120520[/import]

Hi Joshua,

Sorry to resurrect this year-old thread, but I wanted to check whether the conclusion on this thread is still the case, and if not, get your opinion on something.

First, to confirm this thread, is it still the case that calling network.request() during an application suspend or exit event on iOS will not fire the request?  I don’t care about the callback, I’m just interested in whether the request would be sent.

Here’s why I’m asking.  I notice that if I make too many calls to network.request(), the UI can sometimes lag (especially on older devices).  I think this is because Corona is single-threaded.  I’m trying to think about the best way to work around it.  One idea that comes to mind is to wait to send some of my network requests until the application suspends or exits.  For these particular requests, I’m just POSTing some data.  I don’t mind not receiving a callback, I just want the POST to be attempted reliably.  Is that not possible?

If not, then I’m curious if you have any other suggestions for avoiding UI lagging during network requests (aside from using Enterprise)?

Thanks.

  • Andrew

Hi Joshua,

Sorry to resurrect this year-old thread, but I wanted to check whether the conclusion on this thread is still the case, and if not, get your opinion on something.

First, to confirm this thread, is it still the case that calling network.request() during an application suspend or exit event on iOS will not fire the request?  I don’t care about the callback, I’m just interested in whether the request would be sent.

Here’s why I’m asking.  I notice that if I make too many calls to network.request(), the UI can sometimes lag (especially on older devices).  I think this is because Corona is single-threaded.  I’m trying to think about the best way to work around it.  One idea that comes to mind is to wait to send some of my network requests until the application suspends or exits.  For these particular requests, I’m just POSTing some data.  I don’t mind not receiving a callback, I just want the POST to be attempted reliably.  Is that not possible?

If not, then I’m curious if you have any other suggestions for avoiding UI lagging during network requests (aside from using Enterprise)?

Thanks.

  • Andrew