Network.Download after switching scenes

Hey Corona-users,

At the moment I’m working on an application for my internship company but I am kinda stuck on something.

My app involves displaying the portfolio of an advertising agency with the possibility of switching between big projects, news-items and small portfolio-items. At this moment the application checks if the user already got the necessary images for the desired items and if not, it downloads them using the network.download function. Now, when the user is at the news-item screen which starts the download of the needed images and he switches to a different scene before the download has finished and switches back to the news-item screen it becomes all buggy because the downloaded images can’t be inserted into the imgGroup anymore.

To make things a bit clear:

  1. The user start the app and comes out at the news-page. This starts the download of the news-images which need to be shown and aren’t already in the documentsDirectory.
  2. The user switches to the projects-page and after that quickly switches back to the news-page before the download has finished.
  3. Now, while back on the news-page, the download finishes which continues the networklistener. Inside the networklistener it creates the image (display.newImageRect) and after that it inserts it into an imgGroup. The problem is now because of the scene switching it can’t insert the image in the imgGroup (apparently a nil value).

How am I able to fix this? I managed to do nothing inside the networklistener while the user is not on the right scene but it really becomes messy when the user leaves the news-page and comes back on it before the download has finished…

Any help on this is really appreciated.

If any code is needed tell me. I didn’t include it right away because of my code being a real mess after struggling with this for days. [import]uid: 189912 topic_id: 34652 reply_id: 334652[/import]

  • You have two scene:
  • scene1 : the scene you call the network.download
  • scene2 : the scene that you switch to
  • On Scene1: when call the network.download, you need a global variable to check that you are downloading it, like : _G.isDownload = true
  • when download complete, just check current scene name :

local currentSceneName = storyboard.getCurrentSceneName() if currentSceneName == "scene1" then -- insert image to scene view else -- remove image so it can't show to screen if you on other scene end

  • you need the variable _G.isDownload to avoid call methor network.download two times, if you switch to scene2 and back to scene1. Just check

if not \_G.isDownload then -- call methor download, and set: \_G.isDownload = true else --do nothing,wait for download complete, it will trigger the listener end [import]uid: 9190 topic_id: 34652 reply_id: 137708[/import]

Thanks for your reply khanh.dq.

However, this is not the problem here. It is not downloading the same image twice but the download which started the first time while at that scene and finishes during the second visit at the scene messes it up. Like I noted in my first post, it says it can’t insert the downloaded (and created image) into the imgGroup. When I check for existence of imgGroup it doesn’t give an error but when I check for a possibility to insert in into the imgGroup it gives the error, like it doesn’t see the imgGroup as a real group in which items can be inserted.

I’ve been struggling all day with this now and I sort of temporarily “fixed” it now by checking if imgGroup.insert is set (exists) and if not, it just keeps showing a preloader. This won’t show the image until the scene is refreshed though but it doesn’t put the image in the middle of my screen without being able to do anything with it like it did before…

The problem ain’t fixed yet but this doesn’t make it a big mess anymore.

But, if any of you knows how I can fix it please tell me, I’ve been staring at the code now for hours and it is starting to drive me ‘a bit’ crazy. :stuck_out_tongue:

Kind regards,
René [import]uid: 189912 topic_id: 34652 reply_id: 137717[/import]

Do as I said. When the download completed, let the listener check currentSceneName. If it is the scene you call download methor (we call it “scene1”), insert image to the scene.view. Because the currentScene is scene1, the insert can’t error.
Sample code: In “scene1”

function scene:downloadImage()  
 -- call download function with listener  
 network.download(url,listener)  
  
 -- remember to return the image to listener when download completed  
 listener = function(image)  
 local currentScene = storyboard.getCurrentSceneName()  
 if currentScene == "yourSceneName" then  
 self.view:insert(image)  
 -- or insert to your imgGroup  
 --[[  
 because current scene is your scene,  
 it mean you back to your scene  
 so you must create imgGroup again, it can't be nil  
 ]]  
 else  
 -- download finish when you in other scene, so we remove it  
 image:removeSelf()  
 end  
 end  
end  

You don’t show me how you create the imgGroup, so I can’t see what is the error.

  • As I guess, your imgaGroup nil because you purge the new-pages scene when jump to project-page scene. PurgeScene only destroy object, not this reference.
    If you write:
 scene.imgGroup = display.newGroup()  

when you purge the scene, the display object of scene.imgGroup will be remove, but the scene.imgGroup is not nil, so you can’t insert.
In my project, i used to code:

function scene:createScene(event)  
 --[[  
 -- call create group on createScene  
 -- createScene called every time you go to scene, if the scene.view don't exist  
 -- so it will create your group again if you remove it by the pugreScene  
 --]]  
 scene.imgGroup = display.newGroup()  
end  
  
-- purge scene after exit scene if need  
function scene:didExitScene(event)  
 storyboard.purgeScene("yourScene")  
end  
  
function scene:destroyScene(event)  
 --[[  
 --remember the scene table is not destroy if use purgeScene  
 --so must set the reference imgGroup to nil if you purgeScene  
 --or use storyboar.removeScene to auto clear scene  
 --read corona dock for more   
 --]]   
 scene.imgGroup = nil  
end  

Hope this help. [import]uid: 9190 topic_id: 34652 reply_id: 137808[/import]

Thanks khanh.dq!

Had to change it a bit here and there but it works almost completely now. I’ll manage to get the rest working by myself. Thanks a lot!

Cheers,
René [import]uid: 189912 topic_id: 34652 reply_id: 138077[/import]

  • You have two scene:
  • scene1 : the scene you call the network.download
  • scene2 : the scene that you switch to
  • On Scene1: when call the network.download, you need a global variable to check that you are downloading it, like : _G.isDownload = true
  • when download complete, just check current scene name :

local currentSceneName = storyboard.getCurrentSceneName() if currentSceneName == "scene1" then -- insert image to scene view else -- remove image so it can't show to screen if you on other scene end

  • you need the variable _G.isDownload to avoid call methor network.download two times, if you switch to scene2 and back to scene1. Just check

if not \_G.isDownload then -- call methor download, and set: \_G.isDownload = true else --do nothing,wait for download complete, it will trigger the listener end [import]uid: 9190 topic_id: 34652 reply_id: 137708[/import]

Thanks for your reply khanh.dq.

However, this is not the problem here. It is not downloading the same image twice but the download which started the first time while at that scene and finishes during the second visit at the scene messes it up. Like I noted in my first post, it says it can’t insert the downloaded (and created image) into the imgGroup. When I check for existence of imgGroup it doesn’t give an error but when I check for a possibility to insert in into the imgGroup it gives the error, like it doesn’t see the imgGroup as a real group in which items can be inserted.

I’ve been struggling all day with this now and I sort of temporarily “fixed” it now by checking if imgGroup.insert is set (exists) and if not, it just keeps showing a preloader. This won’t show the image until the scene is refreshed though but it doesn’t put the image in the middle of my screen without being able to do anything with it like it did before…

The problem ain’t fixed yet but this doesn’t make it a big mess anymore.

But, if any of you knows how I can fix it please tell me, I’ve been staring at the code now for hours and it is starting to drive me ‘a bit’ crazy. :stuck_out_tongue:

Kind regards,
René [import]uid: 189912 topic_id: 34652 reply_id: 137717[/import]

Do as I said. When the download completed, let the listener check currentSceneName. If it is the scene you call download methor (we call it “scene1”), insert image to the scene.view. Because the currentScene is scene1, the insert can’t error.
Sample code: In “scene1”

function scene:downloadImage()  
 -- call download function with listener  
 network.download(url,listener)  
  
 -- remember to return the image to listener when download completed  
 listener = function(image)  
 local currentScene = storyboard.getCurrentSceneName()  
 if currentScene == "yourSceneName" then  
 self.view:insert(image)  
 -- or insert to your imgGroup  
 --[[  
 because current scene is your scene,  
 it mean you back to your scene  
 so you must create imgGroup again, it can't be nil  
 ]]  
 else  
 -- download finish when you in other scene, so we remove it  
 image:removeSelf()  
 end  
 end  
end  

You don’t show me how you create the imgGroup, so I can’t see what is the error.

  • As I guess, your imgaGroup nil because you purge the new-pages scene when jump to project-page scene. PurgeScene only destroy object, not this reference.
    If you write:
 scene.imgGroup = display.newGroup()  

when you purge the scene, the display object of scene.imgGroup will be remove, but the scene.imgGroup is not nil, so you can’t insert.
In my project, i used to code:

function scene:createScene(event)  
 --[[  
 -- call create group on createScene  
 -- createScene called every time you go to scene, if the scene.view don't exist  
 -- so it will create your group again if you remove it by the pugreScene  
 --]]  
 scene.imgGroup = display.newGroup()  
end  
  
-- purge scene after exit scene if need  
function scene:didExitScene(event)  
 storyboard.purgeScene("yourScene")  
end  
  
function scene:destroyScene(event)  
 --[[  
 --remember the scene table is not destroy if use purgeScene  
 --so must set the reference imgGroup to nil if you purgeScene  
 --or use storyboar.removeScene to auto clear scene  
 --read corona dock for more   
 --]]   
 scene.imgGroup = nil  
end  

Hope this help. [import]uid: 9190 topic_id: 34652 reply_id: 137808[/import]

Thanks khanh.dq!

Had to change it a bit here and there but it works almost completely now. I’ll manage to get the rest working by myself. Thanks a lot!

Cheers,
René [import]uid: 189912 topic_id: 34652 reply_id: 138077[/import]