How to cancel network.download when changing scene using storyboard?

Hi!

For example when downloading user images from Facebook I use the network.download function. I check for every pic if it’s already in the systems.DocumentsDirectory, if not then start the download.

How can I make sure all downloads are canceled when a user leaves the scene?

If not all images are downloaded, the pics will get shown on the newly entered scene. Which is very annoying.

thx

Best would be to wait until the download is finished before allowing the user to do anything else.

When download starts: native.setActivityIndicator(true)

When download finished: native.setActivityIndicator(false)

You can also check if you are still in the scene when download is finished.

if storyboard.getCurrentSceneName == “mySceneName” then

   – show image

end

Thx, but what will happen with a bad connection… the user will get stuck on the screen and needs to shutdown the app hard?

I see there is network.cancel function, maybe there is a way to loop tru all the running download events?

You have an error listener in the NetworkListener. See http://docs.coronalabs.com/daily/api/library/network/download.html

If it times out or there is an error it will call it. So you have to hide your activity indicator in there as well.

Good point, thx gonna try this out.

Best would be to wait until the download is finished before allowing the user to do anything else.

When download starts: native.setActivityIndicator(true)

When download finished: native.setActivityIndicator(false)

You can also check if you are still in the scene when download is finished.

if storyboard.getCurrentSceneName == “mySceneName” then

   – show image

end

Thx, but what will happen with a bad connection… the user will get stuck on the screen and needs to shutdown the app hard?

I see there is network.cancel function, maybe there is a way to loop tru all the running download events?

You have an error listener in the NetworkListener. See http://docs.coronalabs.com/daily/api/library/network/download.html

If it times out or there is an error it will call it. So you have to hide your activity indicator in there as well.

Good point, thx gonna try this out.