Downloading several files but one by one?

Hi,

I’m having a problem with downloading several files (200 to be exact). The problem might occur because my server doesn’t allow that many request from the same IP however if I set a delay in the network.download it works great however I would like for network.download to download the files one by one i.e. the download starts and the next download should not begin before the first one is completed.

function nw.downloadCategory() sqlite.openDatabase() local cats = sqlite.downloadCategories() -- Makes sure the folders exists nw.verifyFolders(cats) local function downloadImages() local i = #cats while cats[i] do local images = sqlite.getFileNames(cats[i].categoryID) for j = 1, #images, 1 do local path = "assets/images/" .. string.lower(cats[i].category) .. "/" local filename = string.lower(images[j].word..".jpg"):gsub("%s+", "") -- Removes all the spaces in the word function p() local params = {} params.progress = true print("Downloading ...") network.download( "http://jefecitostudios.com/languageacademy/"..images[j].imageLocation, "GET", networkListener, params, path .. filename, system.DocumentsDirectory ) end timer.performWithDelay(j \* i \* delay, p) end i = i - 1 end end timer.performWithDelay(50, downloadImages) end

The thing about LUA is that it does not wait for each process to complete. I’m sure it should be like this but it has caused me a lot of problems, especially when I’m used to program in C# where you have to wait for each process to complete or implement multi-threading. I can see the advantages why LUA uses and I understand that it’s event-driven so when I draw graphics and such it all makes sense but not in this case.

Is there any way to download the files one by one? Is it possible to make LUA wait for each process to complete before it begins with the next one?

I guess I can set a flag in the transaction listener and when it’s done the flag is set back and if the flag is not set back the index (i and j) will not increase (or decrease).

Best regards,

Tomas

Rather than using 

for j = 1, #images, 1 do

perhaps you could write this as a recursive function which calls itself through the networkListener(). Your file names appear to be sequential and your recursive function could check for the existance of next file on the server (in sequence) and fire off another download request.

Just an idea. Interesting challenge though. Good luck!

Hi Ksan,

thanks, that’s what I did now and now it downloads one by one. Also it will be easier to implement a loading screen this way.

function nw.downloadCategories() sqlite.openDatabase() local cats = sqlite.downloadCategories() -- Makes sure the folders exists nw.verifyFolders(cats) files = {} files = sqlite.getFileNames() if #files \> 0 then downloadImage(files[counter]) end end function downloadImage(image) local path = "assets/images/" .. image.category .. "/" local filename = string.lower(image.word:gsub("%s+", "")) .. ".jpg" local params = {} params.progress = true network.download( "http://jefecitostudios.com/languageacademy/"..image.imageLocation, "GET", networkListener, params, path .. filename, system.DocumentsDirectory ) end

And in the networkListener:

elseif ( event.phase == "ended" ) then print("Downloaded") counter = counter + 1 if counter \<= #files then downloadImage(files[counter]) end end

Thanks for the help!

Best regards,

Tomas

Rather than using 

for j = 1, #images, 1 do

perhaps you could write this as a recursive function which calls itself through the networkListener(). Your file names appear to be sequential and your recursive function could check for the existance of next file on the server (in sequence) and fire off another download request.

Just an idea. Interesting challenge though. Good luck!

Hi Ksan,

thanks, that’s what I did now and now it downloads one by one. Also it will be easier to implement a loading screen this way.

function nw.downloadCategories() sqlite.openDatabase() local cats = sqlite.downloadCategories() -- Makes sure the folders exists nw.verifyFolders(cats) files = {} files = sqlite.getFileNames() if #files \> 0 then downloadImage(files[counter]) end end function downloadImage(image) local path = "assets/images/" .. image.category .. "/" local filename = string.lower(image.word:gsub("%s+", "")) .. ".jpg" local params = {} params.progress = true network.download( "http://jefecitostudios.com/languageacademy/"..image.imageLocation, "GET", networkListener, params, path .. filename, system.DocumentsDirectory ) end

And in the networkListener:

elseif ( event.phase == "ended" ) then print("Downloaded") counter = counter + 1 if counter \<= #files then downloadImage(files[counter]) end end

Thanks for the help!

Best regards,

Tomas