Busy-wait for network.download() finished? (Synchronous file download)

I have a small file, which is a required file for the app to function at all, and must be ready before creating the first view.

Because of loading other things asynchronously, and this file having to be ready before those, I can’t download this file asynchronously, because that would make the error checking, view creating and redirection to another view if error EXTREMELY complex.

Corona is usually so excellent that I can’t believe there’s no oneliner for this, let alone an example on the network.download manual page.

The problem I’m having with the docs is that it says it’s possible and yet has a REQUIRED listener parameter.

Loading synchronously means: don’t execute the next line of code until the loading has finished. I just can’t see how Lua would know to do that if asked to redirect to a listener.

So: I want to download a file url to a filename in the temporary directory that I specify, and then on the next line load that file into my app. Which is the best practice method?

the sockets lib can do synchronous, fe: http://docs.coronalabs.com/daily/api/library/socket/index.html

however, there are good reasons to go ahead and restructure to handle async too, clearest example is network failure/timeout/other problems. sooner or later, either way sync/async, you’re going to have to deal with potentially not getting the file.

a “simple” way to handle async loading is if using storyboard/composer: make a “do-nothing” splash scene that starts the download, then have listener do either:
gotoScene(“theNextRealSceneOnSuccess”)
or
gotoScene(“aWarningMessageSceneThatWillThenExitAfterUserPushesOkButton”)

you could then also use an enterFrame listener to animate a progress bar or such (if you wished) while waiting

fwiw,hth

Thanks! But I’m not going to restructure the app for this. I already have login data and other data that is handled this way. I can redirect from main.lua to View1.lua and have a check there that can trigger a gotoScene to another view, but the asyncronicity stops there. Also, I have a client. I can’t just add a splash screen when it’s not in the spec to have one.

It says on Rob Miracle’s blog that Corona supports synchronous file downloads, my only problem is finding where it says HOW. :) 

The sync file downloads are done using the socket library. 

Rob

Ah. I understood the first 1-2-3 list in your post as socket being used or else async or sync requests. Apparently I was too stressed yesterday to read properly, the Download Remote Image example in the socket documentation should work well. Just load the local file instead of display.newImage?

the sockets lib can do synchronous, fe: http://docs.coronalabs.com/daily/api/library/socket/index.html

however, there are good reasons to go ahead and restructure to handle async too, clearest example is network failure/timeout/other problems. sooner or later, either way sync/async, you’re going to have to deal with potentially not getting the file.

a “simple” way to handle async loading is if using storyboard/composer: make a “do-nothing” splash scene that starts the download, then have listener do either:
gotoScene(“theNextRealSceneOnSuccess”)
or
gotoScene(“aWarningMessageSceneThatWillThenExitAfterUserPushesOkButton”)

you could then also use an enterFrame listener to animate a progress bar or such (if you wished) while waiting

fwiw,hth

Thanks! But I’m not going to restructure the app for this. I already have login data and other data that is handled this way. I can redirect from main.lua to View1.lua and have a check there that can trigger a gotoScene to another view, but the asyncronicity stops there. Also, I have a client. I can’t just add a splash screen when it’s not in the spec to have one.

It says on Rob Miracle’s blog that Corona supports synchronous file downloads, my only problem is finding where it says HOW. :) 

The sync file downloads are done using the socket library. 

Rob

Ah. I understood the first 1-2-3 list in your post as socket being used or else async or sync requests. Apparently I was too stressed yesterday to read properly, the Download Remote Image example in the socket documentation should work well. Just load the local file instead of display.newImage?

Marked this as unsolved, since I tried the Download Image example from the socket library link (http://docs.coronalabs.com/api/library/socket/index.html) and open() complains that the file doesn’t exist. Of course it doesn’t exist, that’s why I’m downloading it!

Runtime error

/Users/henrikerlandsson/Documents/CORONA-APPAR/TellingEksjo/view0.lua:19: bad argument #1 to ‘open’ (string expected, got nil)

Also, how do I check that it was downloaded correctly?

Can you post some code?

Thanks

Rob

Found it. I was focused on the file since that was the error message, but it was the wrong basepath, sorry about that.

So, how do I check that the file download was completed? Does it delete the file if download was interrupted or does it throw an exception that I can catch?

You’ll want to use the progress listener.  Take a look at this snippet from the docs:

http://docs.coronalabs.com/api/library/network/download.html#examples

Best.

Marked this as unsolved, since I tried the Download Image example from the socket library link (http://docs.coronalabs.com/api/library/socket/index.html) and open() complains that the file doesn’t exist. Of course it doesn’t exist, that’s why I’m downloading it!

Runtime error

/Users/henrikerlandsson/Documents/CORONA-APPAR/TellingEksjo/view0.lua:19: bad argument #1 to ‘open’ (string expected, got nil)

Also, how do I check that it was downloaded correctly?

Can you post some code?

Thanks

Rob

Found it. I was focused on the file since that was the error message, but it was the wrong basepath, sorry about that.

So, how do I check that the file download was completed? Does it delete the file if download was interrupted or does it throw an exception that I can catch?

You’ll want to use the progress listener.  Take a look at this snippet from the docs:

http://docs.coronalabs.com/api/library/network/download.html#examples

Best.