Prevent the loading of a remote Image once the scene is removed

I am remotely loading an image in a scene.
If I switch the scene before the image is loaded, I can see the image on the next scene.

I have other local images along with the remotely loaded image. So I kept the following condition, but it doesn’t help.

local localImage = display.newRect(...);

  local function networkListener(event)
    if (event.isError) then
      print ("Network error - download failed");
    else
      if localImage ~= nil then
           display the remotely loaded image
      end
    end
  end

  display.loadRemoteImage(
    url, 
    "GET", 
    networkListener, 
   ...
  );

How do I prevent this?

This is how i do it … and not only for loading images for any function that i might forget to shut down somehow

you have to declare a global variable called for example currentSceneName in main.lua

and at the top of each scene set this variable to the scene name
currentSceneName = "xyz"

finally inside your function put this line:

local function networkListener(event)
     if currentSceneName~="xyx" then
           return true
     end
    if (event.isError) then
      print ("Network error - download failed");
    else
      if localImage ~= nil then
           display the remotely loaded image
      end
    end
  end

This happens whenever a function you called might still work after you leave a scene
specially when calling network.download or network.request

Hope this helps

2 Likes

For me this worked.

local function networkListener(event)
    if (event.isError) then
      print ("Network error - download failed");
    else
      if currentSceneName =="xyx" then
        display the remotely loaded image
      else
        event.target:removeSelf();
      end
    end
  end

There is an issue though.

If the image is supposed to be loaded in one scene. The user goes to next scene and comes back to this, and it’s now only that the image is loaded from the earlier instance, you will see this image as the condition will hold true. Even though you went to another scene and came back.