network.download not entering event listener

Hi everyone,

I’m at my wits end here with this one.  I’m trying to download an image file from an online folder and one of three things happen.

  1. The event listener that is being called during the “progress” period does not call and the file is never downloaded.

  2. The event listener is called but the event.isError is called 3 times and I also get this message

Error: unable to get the receiver data from the DB!

  1. The Error: unable to get the receiver data form the DB! is printed to the console and the event listener is not called. I have absolutely no idea what this means as I’m just learning the networking functionality of Corona.  I do know that it only occurs when I have already attempted to download the file previously and if the file is actually in the online folder.

The Error does not occur if the file is not in the online folder but the event listener’s event.isError is still called 3 times.

Also, I am trying to download this after I have successfully downloaded a .json file and saved it to the system.DocumentsDirectory (code omitted).  I’m not sure if making a network request previously affects this at all but I thought I’d mention it just in case.

Please help.

Here is the code.

[lua]local eventDownloadListener

local downloadParams = {progress = true, timeout = 10}

local nameOfFile = “events/eventName.png”

local URLPath = “http://myURLpath/eventPictures/eventName.png

local directory = system.DocumentsDirectory

network.download(URLPath, “GET”, eventDownloadListener, downloadParams, nameOfFile, directory)

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 the file”)

     elseif phase == “began” then

          print(“Starting to download file”)

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

          print(“File has downloaded”)

     end

end[/lua]

This is just a guess, but try changing line 10 to this:

eventDownloadListener = function(event)

If the code is actually what you posted here (in terms of positioning), the problem seems to be that you’re creating the function after the call has been made.

Lua code gets executed from top to bottom.

What happens here is that you declare your local variables, you call network.download, and then you assign the function to one of the variables.

Try placing network.download at the last line (after the function) and it should work.

Thanks elbowroomapps and RagdogStudios for the replies!

So I tried elbowroomapps suggestion and there was no difference in functionality.

RagdogStudios, I tried your suggestion and the problem still persisted.  I am forward declaring that function though so I don’t think that was the cause for it.

The code is actually part of a larger project that would be too large to post everything.  What I’m doing is refreshing the downloads from a scrollView.  When the user pulls down far enough on the scroll view it activates a function that checks whether a file needs to be downloaded. The scrollView part works flawlessly.  Everything is actually inside functions and the problem has been isolated to what I posted.

I just can’t understand why (when the image exists at the URL path) that the eventDownloadListener is not being called.  And I’m even more confused with why it’s called 3 times when the file doesn’t exist.

You’re just testing for if phase but you haven’t declared that variable. 

You need to either do a local phase = event.phase at the top of your function or change your tests to if event.phase

(Shakes head in disbelief)

I didn’t follow the K.I.S.S. method very well on this one. 

Thanks ingemar for the response!  Sometimes you just need someone else to take a look at the code.

Thanks again elbowroomapps and RagdogStudios for your replies too!

Been there. Done that  :wink:

This is just a guess, but try changing line 10 to this:

eventDownloadListener = function(event)

If the code is actually what you posted here (in terms of positioning), the problem seems to be that you’re creating the function after the call has been made.

Lua code gets executed from top to bottom.

What happens here is that you declare your local variables, you call network.download, and then you assign the function to one of the variables.

Try placing network.download at the last line (after the function) and it should work.

Thanks elbowroomapps and RagdogStudios for the replies!

So I tried elbowroomapps suggestion and there was no difference in functionality.

RagdogStudios, I tried your suggestion and the problem still persisted.  I am forward declaring that function though so I don’t think that was the cause for it.

The code is actually part of a larger project that would be too large to post everything.  What I’m doing is refreshing the downloads from a scrollView.  When the user pulls down far enough on the scroll view it activates a function that checks whether a file needs to be downloaded. The scrollView part works flawlessly.  Everything is actually inside functions and the problem has been isolated to what I posted.

I just can’t understand why (when the image exists at the URL path) that the eventDownloadListener is not being called.  And I’m even more confused with why it’s called 3 times when the file doesn’t exist.

You’re just testing for if phase but you haven’t declared that variable. 

You need to either do a local phase = event.phase at the top of your function or change your tests to if event.phase

(Shakes head in disbelief)

I didn’t follow the K.I.S.S. method very well on this one. 

Thanks ingemar for the response!  Sometimes you just need someone else to take a look at the code.

Thanks again elbowroomapps and RagdogStudios for your replies too!

Been there. Done that  :wink: