Network Event Listener called after error event is processed

Hi everyone,

So I have an interesting problem that’s driving me nuts.  I’m trying to download an image file from an online folder from my own site and when I run through the download process the event listener is called multiple times AFTER the event.isError already took place.  I’ve purposely deleted the file it’s trying to download in order to test the network error chunk of this code.

I really don’t want the network event listener (eventDownloadListener) to be called that many times after the download isError was set to true so could anyone help on this?

What my code does is it checks the contents of a table to see if the names of the files needed coordinates to the files on the phone; if there is no file on the phone then the network.download() call is envoked and the app waits until the file is downloaded until it tries downloading the next one.  Only 1 download is requested from the table so what is happening is it’s trying to download the non existent file then it re-tries one more time and then after that it is supposed to stop.  That is when the network event listener is being called 5 more times.

Here is the code.

[lua]function eventDownloadListener(event)

     if (event.isError or event.status == 404) then – If there is any kind of error in the download process

          print("There has been an error downloading event image "…eventName)

          numEventImgError = numEventImgError + 1

          nextEventImageHandler()

     elseif phase == “began” then 

          print("Starting to download event "…eventName)

     elseif phase == “ended” then – When the download has occured successfully

          print(“Event “…eventName…” has downloaded”)

          nextEventImageHandler()

     end

end[/lua]

This function re-directs what happens after the download based off of how many downloads are left

NOTE: the [lua]numEventImgToDownload[/lua] variable is calculated in another function that is exactly the same as the [lua]downloadEventImages()[/lua] function only without the network call. It is set to 1 in this case along with the [lua]numDownloadAttempts[/lua]

[lua]function nextEventImageHandler()

     numEventImgToDownload = numEventImgToDownload - 1

     print("# event images left to download = "…numEventImgToDownload)

     print("# of errors = "…numEventImgError)

     print("# of download attempts left = "…numDownloadAttempts)

     if numEventImgToDownload > 0 then

          downloadEventImages() – Download the next image

     elseif numEventImgToDownload == 0 and numEventImgError == 0 then

          downloadingEventImages = false

          numDownloadAttempts = resetAttempts

     elseif numEventImgToDownload == 0 and numEventImgError > 0 and numDownloadAttempts > 0 then – Try      downloading failed files again

          numDownloadAttempts = numDownloadAttempts - 1

          downloadingEventImages = true

          numEventImgError = 0

          getNumberOfEventDownloads() – Start the whole downloading process over

     elseif numEventImgToDownload == 0 and numEventImgError > 0 and numDownloadAttempts == 0  then

          print(“Failed to download all files: Terminating attempts”)

          downloadingEventImages = false

          numDownloadAttempts = resetAttempts

          eventName = “noImage”

     end

end[/lua]

This function moves through the eventTable and checks to see if a file is needed to be downloaded.

[lua]function downloadEventImages()

     for i = 1, #extVar.eventTable do

          local path = system.pathForFile(“events/”…extVar.eventTable[i].name…extVar.prefix…".png",      system.DocumentsDirectory)

          local localPath = system.pathForFile(“images/”…extVar.eventTable[i].name…extVar.prefix…".png", system.ResourceDirectory)

          local test = nil

          local localTest = nil

          if path then test = io.open(path) end

          if localPath then localTest = io.open(localPath) end

          if test then – If the path exists then don’t try downloading the image

               test:close()

               if localTest then localTest:close() end

               print(“File “…extVar.eventTable[i].name…” exists in the Documents Directory”)

          elseif localTest then

               localTest:close()

               if test then test:close() end

               print(“File “…extVar.eventTable[i].name…” exists in the Resource Directory”)

          else – if either path doesn’t exist then download the file

               print(extVar.eventTable[i].name…" will be downloaded")

               eventName = extVar.eventTable[i].name

               local nameOfFile = extVar.eventTable[i].name…extVar.prefix…".png"

               local URLPath =     “http://myURLpath/eventPictures/"..extVar.eventTable[i].name..extVar.prefix..".png

               local directory = system.DocumentsDirectory

               print("fileName = "…nameOfFile)

               print("fileURL = "…URLPath)

               network.download(URLPath, “GET”, eventDownloadListener, {progress = true, timeout = 5}, nameOfFile, directory)

               break – Wait for the file to download and then download the next file if there is one

          end

     end

end[/lua]

I solved the issue.  I wasn’t declaring the “phase” variable in the eventDownloadListener() function. Simple mistake, simple solution.

I solved the issue.  I wasn’t declaring the “phase” variable in the eventDownloadListener() function. Simple mistake, simple solution.