Need to Pause All Other Functions Until All Images are Loaded From my WebServer

Hi,

Scenario 1 (preferred outcome)

I’m building a business app with a page that will load a number of remote images from my web server. The issue I’m running into is that the tableview builds quicker than I the images are downloading. Ideally, I’d like the user to be able to scroll the page while as the images load into the view.

I’ve a read a few posts and tutorials  and the documentation on the differences between using synchronous versus asynchronous loading. The closest to an answer was this post (busy-wait-for-networkdownload-finished-synchronous-file-download/).

Scenario 2 (I can live with it for now, if I have to)

Although, not ideal, I’d be fine with having a loading screen until the images have had a chance to download but I’m banging my head against a wall having the same problem. The gotoScene call is happening before the images are finished loading.

If I just run the code to load the images, it completes the full download without any issues. Also, note the reference to mc:run is the coronium code I’m using to get the URL data back from my database.

Here’s my code:

LoadImages Scene:

... --------\< START \> GET THE FILES NAMES FROM THE SERVER \< START \>---------------- local myData local function onRun( event ) if not event.error then print( event.result ) myData = event.result for i=1, #myData do print( myData[i].submissionID, " ", myData[i].question, " ", myData[i].item1, " " , myData[i].item1PicURI, " ", myData[i].item2, " ", myData[i].item2PicURI, " " ) ----------------------MAPPING FILE ADDRESSES FOR IMAGES----------------- files = {} files[1] = "http://162.243.157.98/files/" .. myData[i].item1PicURI files[2] = "http://162.243.157.98/files/" .. myData[i].item2PicURI newFileName = {} newFileName[1] = myData[i].submissionID .. "-image1.jpg" newFileName[2] = myData[i].submissionID .. "-image2.jpg" ----------------------MAPPING FILE ADDRESSES FOR IMAGES----------------- ----------------------\<START\> NETWORK REQUEST - DOWNLOAD IMAGES\<START\>----------------- for i=1, #files do local function networkListener( event ) if ( event.isError ) then print( "Network error!") elseif ( event.phase == "began" ) then if event.bytesEstimated \<= 0 then print( "Download starting, size unknown" ) else print( "Download starting, estimated size: " .. event.bytesEstimated ) end elseif ( event.phase == "progress" ) then if event.bytesEstimated \<= 0 then print( "Download progress: " .. event.bytesTransferred ) else print( "Download progress: " .. event.bytesTransferred .. " of estimated: " .. event.bytesEstimated ) end elseif ( event.phase == "ended" ) then print( "Download complete, total bytes transferred: " .. event.bytesTransferred ) end end local params = {} -- This tells network.request() that we want the 'began' and 'progress' events... params.progress = "download" -- This tells network.request() that we want the output to go to a file... params.response = { filename = newFileName[i], baseDirectory = system.DocumentsDirectory } network.request( files[i], "GET", networkListener, params ) end ----------\< END \> NETWORK REQUEST - DOWNLOAD IMAGES\< END \>----------------- end --END OF #MYDATA FOR LOOP--- end ---END OF IF NOT (EVENT.ERROR) STATEMENT--- end ---END OF onRun FUNCTION--- mc:run( "getMyPosts", userData, onRun ) --------\< END \> GET THE FILES NAMES FROM THE SERVER \< END \>---------------- composer.gotoScene( "imagesLibrary" ) ...

Thanks in advance for the help!

Hi Jeremiah, 

Take a look at the link below for a through discussion on this topic with 2 solutions to the challenge : 

http://forums.coronalabs.com/topic/38541-loading-async-images-and-tableview

I have been using the method shared by @mpappas with great success (see link inside the thread listed above) but the solution shared by @jonjonsson is also great! 

Let us know if you have any questions after reviewing those threads and I will do my best to respond. 

All the best,

Kerem

This is exactly what I needed! Thanks for the help.

Cheers,

Jeremiah

Great to hear. Call out if you need help implementing this approach. All the best.

Hi Jeremiah, 

Take a look at the link below for a through discussion on this topic with 2 solutions to the challenge : 

http://forums.coronalabs.com/topic/38541-loading-async-images-and-tableview

I have been using the method shared by @mpappas with great success (see link inside the thread listed above) but the solution shared by @jonjonsson is also great! 

Let us know if you have any questions after reviewing those threads and I will do my best to respond. 

All the best,

Kerem

This is exactly what I needed! Thanks for the help.

Cheers,

Jeremiah

Great to hear. Call out if you need help implementing this approach. All the best.