I’m not sure what you’re doing exactly, but when I find myself in a situation where I need to make many network requests that do similar things and use a similar listener I do one of two things:
-
Write a single homogeneous listener that can be used for all requests
-
Write a generator/wrapper that both generates a custom listener and issues the request.
https://github.com/roaminggamer/RG_FreeStuff/raw/master/AskEd/2018/04/generatedListener.zip
This is an example of the latter:
local baseURL = "https://raw.githubusercontent.com/roaminggamer/RG\_FreeStuff/master/AskEd/common/cards/" local function createCard( x, y, w, h, num ) local tmp = display.newRect( x, y, w, h ) local function networkListener( event ) if ( event.isError ) then display.remove( tmp ) elseif ( event.phase == "ended" ) then tmp.isVisible = true tmp.fill = { type = "image", filename = event.response.filename, baseDir = event.response.baseDirectory } end end local to = "cardClubs" .. num .. ".png" local from = baseURL .. to local params = {} network.download( from, "GET", networkListener, params, to, system.TemporaryDirectory ) end
It is kind a silly example, but it shows how, knowing a little of what is common in the use cases, I can make a wrapper to generate most of the code I would otherwise write by hand over and over.