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