network downloading and pass variables

For the project that I am working on, I need to download a some images and insert those image to the different display group. Each image should have a ID so I can keep tracking them.

But looks like network.download can not pass a value to the networkListener except filename and directory.

What I did is name the downloaded files by the ID, and then use the file name to track them.

But I’m sure there should be a better way to do it.

I heard Rob said some thing about “anonymous functions and closures” cloud helps, but I have no idea how it should works

http://forums.coronalabs.com/topic/44625-networkrequest-no-way-to-pass-information-to-my-networklistener-function/

http://coronalabs.com/blog/2014/02/18/tutorial-anonymous-functions-and-closures/

I ready those topic but there isn’t a sample code, could any1 plz give me some idea / hit how this code should looks like?

Thanks a lot.

local function networkListener(event) if ( event.phase == "ended" ) then        local fn = event.response.filename         local ID = tonumber(fn:sub(1,1))         ​NImage[ID] = display.newImageRect(event.response.filename, event.response.baseDirectory,320,320)         NIGroup[ID]:insert(​NImage[ID])     end end NIGroup = {} NImage = {} for i = 1, 5 do NIGroup[i] = display.newGroup() network.download("http:xxxxxx/test"..i..".png", "GET",networkListener,i..".png",system.TemporaryDirectory)    end

Maybe something like this:

  local function networkListener(event, ID) if ( event.phase == "ended" ) then        local fn = event.response.filename         ​NImage[ID] = display.newImageRect(event.response.filename, event.response.baseDirectory,320,320)         NIGroup[ID]:insert(​NImage[ID])     end end NIGroup = {} NImage = {} for i = 1, 5 do NIGroup[i] = display.newGroup()   network.download("http:xxxxxx/test"..i..".png", "GET",      function(event) networkListener(event, i); end,      i..".png",system.TemporaryDirectory)    end

network.download needs the address to a function and will only pass an even table to that function.  By wrapping your real networkListener function inside of an anonymous function you can get that event table that’s being passed and send it to your function and at that point, you have access to your other variables, like “i” and send that in as well.

Rob

@Rob   wow, it works like a charm.

Thank you, Rob.

Maybe something like this:

  local function networkListener(event, ID) if ( event.phase == "ended" ) then        local fn = event.response.filename         ​NImage[ID] = display.newImageRect(event.response.filename, event.response.baseDirectory,320,320)         NIGroup[ID]:insert(​NImage[ID])     end end NIGroup = {} NImage = {} for i = 1, 5 do NIGroup[i] = display.newGroup()   network.download("http:xxxxxx/test"..i..".png", "GET",      function(event) networkListener(event, i); end,      i..".png",system.TemporaryDirectory)    end

network.download needs the address to a function and will only pass an even table to that function.  By wrapping your real networkListener function inside of an anonymous function you can get that event table that’s being passed and send it to your function and at that point, you have access to your other variables, like “i” and send that in as well.

Rob

@Rob   wow, it works like a charm.

Thank you, Rob.