When is system.TemporaryDirectory cleared ?

From my findings, it is only after the application has been uninstalled.

I am particularly interested for iOS devices.

I hoped it clears when exiting the application.

I don’t think it always clears when the application exits.  I think it’s up to the OS to clear it when it chooses, which might be when it’s running low on disk space, when the system is idle, or any other reason/time.

  • Andrew

Android OS has a “delete data” feature when managing apps, maybe that clears on droid (never tried). But iOS has no feature to delete the apps data (that I know of). Both iOS and Droid  seem to count the apps temp folder for the apps reported data usage :frowning:

One thing you could do perhaps, is at app exit run a loop that deletes all the files in the apps temp folder. The docs to write it and an example (sorta) is here: http://docs.coronalabs.com/api/library/lfs/index.html - looks like a pretty short piece of code.

If you do write that loop (for app exit, or somewhere else), I wouldn’t mind if you posted the code, probably a buncha people wouldn’t mind snagging it. Not sure what I should do, the temp folder is a nice cache area for my app, it just gets big after a while. Maybe I should wait until it hits a certain size, them cull the oldest images out or something like that. hmmm.

I have tried this on the simulator, but it doesn’t clear the temporary directory.

Maybe it works on the device.

local function clearTempDir()     local lfs = require "lfs"     local doc\_path = system.pathForFile( "", system.TemporaryDirectory )     for file in lfs.dir(doc\_path) do         -- file is the current file or directory name         os.remove(file)     end end  

Got it to work!

local function clearTempDir()     local lfs = require "lfs"     local doc\_path = system.pathForFile( "", system.TemporaryDirectory )     local destDir = system.TemporaryDirectory  -- where the file is stored          for file in lfs.dir(doc\_path) do         -- file is the current file or directory name         os.remove( system.pathForFile( file, destDir  ) )     end end  

 Well done!

I don’t think it always clears when the application exits.  I think it’s up to the OS to clear it when it chooses, which might be when it’s running low on disk space, when the system is idle, or any other reason/time.

  • Andrew

Android OS has a “delete data” feature when managing apps, maybe that clears on droid (never tried). But iOS has no feature to delete the apps data (that I know of). Both iOS and Droid  seem to count the apps temp folder for the apps reported data usage :frowning:

One thing you could do perhaps, is at app exit run a loop that deletes all the files in the apps temp folder. The docs to write it and an example (sorta) is here: http://docs.coronalabs.com/api/library/lfs/index.html - looks like a pretty short piece of code.

If you do write that loop (for app exit, or somewhere else), I wouldn’t mind if you posted the code, probably a buncha people wouldn’t mind snagging it. Not sure what I should do, the temp folder is a nice cache area for my app, it just gets big after a while. Maybe I should wait until it hits a certain size, them cull the oldest images out or something like that. hmmm.

I have tried this on the simulator, but it doesn’t clear the temporary directory.

Maybe it works on the device.

local function clearTempDir()     local lfs = require "lfs"     local doc\_path = system.pathForFile( "", system.TemporaryDirectory )     for file in lfs.dir(doc\_path) do         -- file is the current file or directory name         os.remove(file)     end end  

Got it to work!

local function clearTempDir()     local lfs = require "lfs"     local doc\_path = system.pathForFile( "", system.TemporaryDirectory )     local destDir = system.TemporaryDirectory  -- where the file is stored          for file in lfs.dir(doc\_path) do         -- file is the current file or directory name         os.remove( system.pathForFile( file, destDir  ) )     end end  

 Well done!

Hi thanks for this info, very helpful.

I am having an issue with the image (that was downloaded) staying the same, even after changing the image file on the server.

In other words, if image1.png was downloaded once and then the next day image1.png was changed on the server ( same name but different image ), the app still shows the old image1.png instead of re-downloading it (??) and displaying the new image1.png? Am I mis-understanding how the network.request call works? Shouldn’t it download the image1.png every time it is called?

I clear the temp directory of image1.png right before the network.request() call for the download so I don’t understand why the app still shows the “old” version of image1.png and not the new version of image1.png from the server / download.

Any help would be appreciated.

Cheers!

here is the code I am using:

local function clearTempDir()

                local lfs = require “lfs”

                local doc_path = system.pathForFile( “”, system.TemporaryDirectory )

                local destDir = system.TemporaryDirectory  – where the file is stored

    

                    for file in lfs.dir(doc_path) do

                        – file is the current file or directory name

                        os.remove( system.pathForFile( file, destDir  ) )

                        print("Temp Dir Cleared, removed file: ", file)

                    end

            end    

clearTempDir() – to remove former versions of the images that were downloaded before

                    local params = {}

                            params.progress = true

                    network.download(

                            “http://www.myServer.com/image1.png”,

                            “GET”,

                            image1Handler,

                            params,

                            “image1.png”,

                            system.TemporaryDirectory

                            )

                    

Actually - I read this post / tutorial here:

http://coronalabs.com/blog/2013/03/19/updating-mobile-game-content-in-runtime-guest-post/

Sounds good, also downloading to a custom directory is found here:

http://forums.coronalabs.com/topic/34057-downloading-to-a-custom-directory/

Both excellent resources on this topic, however - my image1.png is still not displaying the new version from the server after being changed.

I will experiment with the XML update process described in the first link I posted in the post. Maybe that will make a difference.

Cheers!

@Appsessed

Your old image is probably being cached by a proxy server.

Try adding:

params.headers={["Cache-Control"] = "max-age=86400, must-revalidate"}

max-age is the maximum age (in seconds) that a cached file can have before being reloaded by the proxy server.

86400 = 1 day (60 seconds * 60 minutes * 24 hours)

After changing this, you might need to run your app twice. The first time it will update the proxy with the new header.

@ingemar

Thanks this fixed the image replacement problem, however (of course) there is now a new error msg coming up when I run the app in the simulator as follows:

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

This pops up whenever it is loading the next image in the que from the server. It does’t happen every time, seems to only happen on first launch.

Is this something I should be concerned about? What does this mean exactly? These images have nothing to do with our DataBase  AND there are no DataBase calls in between the network.download requests for the image assets however this error pops up.

Hmm…

I’m new to all of this, by the way :smiley: , so thanks for all of the help.

Cheers!

Corona sdk caches images based on filename.  Once an app loads image1.png (jpg), the system never gets another copy from the device, even if it is deleted, and re-downloaded.  The only way I know of to have images with the same name work as expected – is they must be in different folders  (the system uses the full pathname as well to determine if it is already cached).

The only solution I know of is to save the file locally with a different name (or keep updating the server names).  You can check the forums, but I believe this is the case still (and intentionally - for high perfomance gaming/caching).

Thanks mpappas. I will do some more research on that. Still wondering what that error message means though.

Cheers!

Corona sdk caches images based on filename.  Once an app loads image1.png (jpg), the system never gets another copy from the device, even if it is deleted, and re-downloaded. 

Appsessed is talking about a different issue.

The Corona caching only occurs while the app is running. As you pointed out, an image is cached when it’s first used and any subsequent calls to newImage() or the like will get the cached image regardless of if it’s been changed or not.

If you remove the app from the background (and make sure your app’s cache is cleared), the images will be re-downloaded again on “first launch”. If a proxy is caching you’ll get the old images, but adding the header I posted above to network.download() fixes that issue.

@Appsessed

You’re talking about the Xcode simulator, right? I don’t think you need to be worried. It’s usually the Simulator cache that’s acting up.

You can try to delete all apps from the simulator with the following command:

(replace “replace-with-your-simulator-version” with the version of the simulator you’re simulating.

rm -rf ~/Library/Application Support/iPhone Simulator/replace-with-your-simulator-version/Applications/\*

CAUTION! As always with the rm -rf command: Be _ extra _ careful that you’ve typed the correct folder location or you might mess things up badly.

@ingemar

Yes the header statement you suggested is working for the behaviour I was looking for, I can now change the image on the server and upon relaunch the new image is loaded - after the max-age stated in the header.

Cheers!