Help required with network.download

I have a server that generates a different image every time a request to a URL is made.
I simply want to pull the image, and once received perform the pull again. Effectively creating a constantly updating image.

I created a function that requests the image. I call this function again once I have received a successful download, or at least I thought I was calling it. No errors are generated.

I get my first image, but that is it.

[code]
local function networkListener( event )
if ( event.isError ) then
print ( “Network error - download failed” )
else
myImage = display.newImage( “test.jpg”, system.TemporaryDirectory, 60, 40 )
getimage()
end

print ( "RESPONSE: " … event.response )
end

function getimage ()
network.download( “http://www.mysite.com:8000/image.jpg”, “GET”, networkListener, “test.jpg”, system.TemporaryDirectory )
end
network.download( “http://www.mysite.com:8000/image.jpg”, “GET”, networkListener, “test.jpg”, system.TemporaryDirectory )
[/code] [import]uid: 74957 topic_id: 26630 reply_id: 326630[/import]

Just had a thought. Is my app caching the image, and not downloading it again? [import]uid: 74957 topic_id: 26630 reply_id: 107899[/import]

I have a guess…

One thing you need to do is declare the getImage() function BEFORE calling it. Lua screws up if there’s no prototype… (forward references)…

So before line 1 of your program add

local getImage

and when you declare the function, just use:

function getImage() – like you have now, DON’T include “local” on the definition

If that doesn’t fix it, add a print statement to the getImage function, so you can determine whether corona is managing to call it at all…
[import]uid: 79933 topic_id: 26630 reply_id: 107901[/import]

Thank you for the reply.

I forgot about LUA wanting functions declared before they are used. I made the change.

It would appear that the image is now being downloaded. I confirmed this by adding a counter to my http server that increments for every image that is downloaded.
I do not know if the temporary image file is not being updated, or if the temporary image file is simply not being reloaded, and instead a cached version is being loaded.

I see the files leaving my server. I see the event handler being called. I do not see the image changing. [import]uid: 74957 topic_id: 26630 reply_id: 107909[/import]

There’s a couple ways I can think of to determine what the problem is that come to mind.

a) you can delete the file every time before reloading it. (This will ensure network download isn’t balking at overwriting a file, and may reset any caching corona is doing of images)

b) You can give the file a new name for every download (just for testing). You’ll end up with a lot of files in the simulator cache, but your dev system has plenty of space to run a quick test(and they can be deleted later)

c) You can put an eyeball on the simulator Sandbox, and look at what is happening to “test.jpg”… Is it updating every couple seconds? Is the image itself when viewed in the image viewer on dev system changed? If you delete the image WHILE THE PROGRAM IS RUNNING does it get replaced with an updated image from the server? (File menu: Show Project Sandbox – test.jpg should be in the tmp folder) [import]uid: 79933 topic_id: 26630 reply_id: 107921[/import]

For me, “c” made the most sense (it didn’t require code modification).
I didn’t realize this folder was readily accessible.
The image is in fact changing in the simulator sandbox. I can see the image deleted and a new one created. If I open the image it shows the correct, updated, information.

So it looks like the image is just not being updated.
I created an incremental counter and had the image drawn at this position. The image moved across screen, it was just never updated. So I know the call to draw the image is taking place, it just draws the last image.

I tried adding
myImage = nil before myImage = display.newImage( “test.jpg”, system.DocumentsDirectory, 0, 0)

I tried storing the file in the Documents directory instead of temporary directory.

I added a button that loads the “test.jpg” file and display it in a new image - it does not load whatever is in the sandbox, it seems to just use the first image ever loaded.

I’m new to LUA. I am not sure if it is simply my coding at fault, a bug in Corona, or a combination of both.

I figure I need to invalidate the image somehow, and force a reload. myImage = nil does achieve this.
An interesting side note is that “Suspend” under the “Hardware” menu does not seem to do anything other than add a progress wheel and dim the display. According to my http server the “suspended” simulator is merrily requesting image after image, albeit still not displaying them. Forcing me to close and reopen the project in order to stop it from wasting bandwidth.
[import]uid: 74957 topic_id: 26630 reply_id: 107928[/import]

http://developer.anscamobile.com/reference/index/displaynewimage
display.newImage( )

"Remarks:
All loaded images are cached. To save texture memory the image in the cache memory is used when it detects an image with the same file name is displayed. This means that loading the same image multiple times doesn’t increase the amount of texture memory used on the device.

A negative side-effect to the image caching is the comparison is based on the file name and not the file content. This means if an image file is displayed and then deleted from the directory, any file loaded after that with the same file name will still display the previous cached image. Use a different file name to avoid this problem.

"

[import]uid: 74957 topic_id: 26630 reply_id: 107931[/import]

mpappas, thanks for your help.

I have files being named sequentially and working as expected.

Once I figure out how to delete the previous file I can go with this method. [import]uid: 74957 topic_id: 26630 reply_id: 107937[/import]

Glad I could help.

For deleting files, corona has the os.remove() api. Haven’t used it myself, but it looks like it will do the job.

http://developer.anscamobile.com/reference/index/osremove

Also, make sure to delete the old image from memory in your final code. For instance, just before creating the new image, if there is an old image, call remove it ( object:removeSelf() - http://developer.anscamobile.com/reference/index/objectremoveself ) and nil the variable out (so lua garbage collects the old object).

Then create / load the new image. If you don’t get rid of the old image completely, you could end up having hundreds (or thousands) of images in memory, and eventually run out of texture memory… [import]uid: 79933 topic_id: 26630 reply_id: 107971[/import]

Thank you for the link.
I had been searching through the File I/O APIs, and system APIs. I was beginning to think there was not an API for deleting a file. [import]uid: 74957 topic_id: 26630 reply_id: 108047[/import]