I would like to create a resource download helper to download assets from JSON file
for example :
for i=1, 1000 do
network.download
end
1.the tasks run on multi-thread or run on system queue?
2.should create a download queue to handle?
I would like to create a resource download helper to download assets from JSON file
for example :
for i=1, 1000 do
network.download
end
1.the tasks run on multi-thread or run on system queue?
2.should create a download queue to handle?
https://docs.coronalabs.com/api/library/network/index.html
download --> https://docs.coronalabs.com/api/library/network/download.html
listener --> https://docs.coronalabs.com/api/library/network/download.html#listener-required
Listener event details --> https://docs.coronalabs.com/api/event/networkRequest/index.html
Pseudo-code follows:
-- Forward Declare download helper function
local downloadNext
-- Read JSON data and convert to numerically indexed table
-- of things to download.
local jsonData = some_function_to_read_json_and_convert_to_table()
local curEntry = 0
local function listener( event )
if( event.isError ) then
print( "Error downloading: ", jsonData[curEntry].src)
return
elseif( event.phase == "ended' ) then
downloadNext() -- call download helper function
end
end
-- Define download helper function
downloadNext = function ()
curEntry = curEntry + 1
if (curEntry > #jsonData ) then
print( "Done downloading files.")
return
end
network.download( jsonData[curEntry].src, "GET", listener,
jsonData[curEntry].dst, system.DocumentsDirectory )
end
The above code assumes your original JSON data was a table in this format; change as needed, but that is up to you:
local origTable = {
{ src = http://docs.coronalabs.com/images/simulator/image1.png", dst = "image1.png" },
{ src = http://docs.coronalabs.com/images/simulator/image2.png", dst = "image2.png" },
{ src = http://docs.coronalabs.com/images/simulator/image3.png", dst = "image3.png" },
...
{ src = http://docs.coronalabs.com/images/simulator/imageN.png", dst = "imageN.png" },
}
Tip: You can have multiple outstanding downloads. It would be easy to adjust the pseudo-code above to handle that. However, this may bog down your device and cause hiccups/slowness.
What about gotchas below? (from native.newTextField())
On Android, due to the fact that Corona runs on multiple threads, getting the
.text
value of a TextField immediately after setting it will not return the correct value. This action should be performed following a short timer or during the next time step.
Have a nice day:)
ldurniat
That isn’t referring to Corona SDK / Solard 2D being multi-threaded. That is saying, the native elements may execute in a separate thread.
It is worth noting that this is pointing out one of the exact problem that folks who aren’t used to working with threaded code get stuck on. Synchronized data.
Your scripts all run in a single thread. I always stomp on the idea of threading when I see it.
Thanks you so much