Wait Download Finishes

Hi everybody, I’m trying to download one configuration file that’s hosted on URL(it can’t be added directly to app because it changes every month). But since I need it to load other images and stuffs on my app, my code should stop until I had this downloaded complete.

I tried different ways and all of then just failed. Here’s the last try:
 

function downloadWait(link,filename)   local function networkListener(event)       isDownloading = true     if ( event.isError ) then       print("Network error - download failed")       isDownloading = false     elseif (event.phase == "began") then       print("Progress Phase: began")       isDownloading = true     elseif ( event.phase == "ended" ) then       print( "file downloaded" )       isDownloading = false     end   end     function file\_exists(name)     path = system.pathForFile(name, system.DocumentsDirectory)     local f=io.open(path,"r")      if f~=nil then io.close(f) return true else return false end   end      if not file\_exists(filename) then     network.download(link,"GET",networkListener,filename)     print("1")       while isDownloading do         print(isDownloading)       end     end end  

But it never enter on the while loop.

Can someone help me ?

PS: Also another problem was that I can’t rewrite a file, maybe it has something on permissions that I forget?

Thanks, Pedro.

You might want to consider starting a native.setActivityIndicator() when you start the download and then turn it off when the download is complete.  It should stop interaction with your app.

http://docs.coronalabs.com/api/library/native/setActivityIndicator.html

Rob

Hi Rob :), thanks for your answer. I added  to my code this native.setActivityIndicator(), but it seems that the networkListener is “slower” then my code is “read”.

Check this:

function downloadWait(link,filename\_,typeOfDownload)   local params = {}   params.progress = "download"   params.response = {         filename = filename\_,         baseDirectory = system.DocumentsDirectory   }   local function networkListener(event)     native.setActivityIndicator(true)     if ( event.isError ) then       print("Network error - download failed")     elseif (event.phase == "began") then       print("Progress Phase: began")       native.setActivityIndicator(true)     elseif ( event.phase == "ended" ) then       print( "file downloaded" )       native.setActivityIndicator(false)     end   end     function file\_exists(name)     path = system.pathForFile(name, system.DocumentsDirectory)     local f=io.open(path,"r")      if f~=nil then io.close(f) return true else return false end   end     if typeOfDownload == "normal" then --do not rewrite      if not file\_exists(filename\_) then       network.request(link,"GET",networkListener,params)      end    elseif typeOfDownload == "rewrite" then      if file\_exists(filename\_) then       local path = system.pathForFile(filename\_, system.DocumentsDirectory)       os.remove(path)      end     network.request(link,"GET",networkListener,params)    end end downloadWait("http://alagarta.com/app/info.txt","info.txt","normal") local path = system.pathForFile("info.txt", system.DocumentsDirectory) file = io.open(path, "r" ) for line in file:lines() do   print(line) end

Terminal:

rV0L5Y5.jpg

I’d suggest having an initial lua file (scene) that has one job - request the download of this file and when it has been successfully received, move onto the first proper scene. That way you’re ensuring you can’t possibly run any code that will crash if this data is missing.

I’ll try this solution, first I’ll try to understand how Corona works with scenes … Do you know where I could read more about it?

Corona (well Lua) by default buffers it’s output. You can’t judge timing things and prints unless you turn off the print buffering.  Try putting this at the top of your main.lua:

io.output():setvbuf(‘no’)

@Nick_Sherman Thanks you ! I used 2 scenes, the first one calls every download needed to load the main Menu, while it don’t finish the downloads it keeps backing to this scene (downloadScene). When the download is ready it goes to menu !

@Rob Oh I didn’t knew that, so its like C (maybe because lua its based on C?). Another question relative to native.setActivityIndicator(), this loading bar that show on my screen will be shown on my phone?

Thanks guys you are helping a lot !

You might want to consider starting a native.setActivityIndicator() when you start the download and then turn it off when the download is complete.  It should stop interaction with your app.

http://docs.coronalabs.com/api/library/native/setActivityIndicator.html

Rob

Hi Rob :), thanks for your answer. I added  to my code this native.setActivityIndicator(), but it seems that the networkListener is “slower” then my code is “read”.

Check this:

function downloadWait(link,filename\_,typeOfDownload)   local params = {}   params.progress = "download"   params.response = {         filename = filename\_,         baseDirectory = system.DocumentsDirectory   }   local function networkListener(event)     native.setActivityIndicator(true)     if ( event.isError ) then       print("Network error - download failed")     elseif (event.phase == "began") then       print("Progress Phase: began")       native.setActivityIndicator(true)     elseif ( event.phase == "ended" ) then       print( "file downloaded" )       native.setActivityIndicator(false)     end   end     function file\_exists(name)     path = system.pathForFile(name, system.DocumentsDirectory)     local f=io.open(path,"r")      if f~=nil then io.close(f) return true else return false end   end     if typeOfDownload == "normal" then --do not rewrite      if not file\_exists(filename\_) then       network.request(link,"GET",networkListener,params)      end    elseif typeOfDownload == "rewrite" then      if file\_exists(filename\_) then       local path = system.pathForFile(filename\_, system.DocumentsDirectory)       os.remove(path)      end     network.request(link,"GET",networkListener,params)    end end downloadWait("http://alagarta.com/app/info.txt","info.txt","normal") local path = system.pathForFile("info.txt", system.DocumentsDirectory) file = io.open(path, "r" ) for line in file:lines() do   print(line) end

Terminal:

rV0L5Y5.jpg

I’d suggest having an initial lua file (scene) that has one job - request the download of this file and when it has been successfully received, move onto the first proper scene. That way you’re ensuring you can’t possibly run any code that will crash if this data is missing.

I’ll try this solution, first I’ll try to understand how Corona works with scenes … Do you know where I could read more about it?

Corona (well Lua) by default buffers it’s output. You can’t judge timing things and prints unless you turn off the print buffering.  Try putting this at the top of your main.lua:

io.output():setvbuf(‘no’)

@Nick_Sherman Thanks you ! I used 2 scenes, the first one calls every download needed to load the main Menu, while it don’t finish the downloads it keeps backing to this scene (downloadScene). When the download is ready it goes to menu !

@Rob Oh I didn’t knew that, so its like C (maybe because lua its based on C?). Another question relative to native.setActivityIndicator(), this loading bar that show on my screen will be shown on my phone?

Thanks guys you are helping a lot !