views:

543

answers:

4

Hi all,

We often cache image and data to improve our iPhone app's performance. But what strategy do you use to manage the cache data, such as delete or update it?

I saved the images to TMP folder, but don't know when should I trigger to "checking out of date cache data and delete it": when the iPhone apps starts, or quits, or in idle time?

+2  A: 

You should delete the cache when the iPhone invokes your "didReceiveMemoryWarning" function.

Michael Aaron Safyan
+1 you may also want to clean out the cache on your own, but doing it in response to a memory warning is critical.
David Gelhar
thanks, but how about storing data to the disk? When we check the cache data to find and delete out-of-date data?
Thanh-Cong Vo
@athanhcong, you can find and delete out of date cache data periodically using NSTimer... you will have to choose a period that results in the best performance... that will be highly dependent on your application. My suggestion is that you profile it.
Michael Aaron Safyan
+1  A: 

When the application launches or quits, the user is usually expecting responsiveness. Choose a minimum amount of real time, like 24 hours, and a minimum amount of idle time, like a minute. If the user has been idle for a minute and it is more than 24 hours from the last cache purge, then clean the cache. If you are keeping track of how much data is cached, then you could factor that in as well. If it has been more than 24 hours and/or there is more than a megabyte in the cache.

If you are sure the application is being quit normally, as opposed to being quit to answer a call or launch another application, then that might also be a good time.

If your application does something that the user has to wait for anyway, but that would not be affected by purging the cache, then that might also be a good time. For example, fetching some data from a server.

drawnonward
A: 

I personally dosn't like if cache is cleaned when i open a application or idle longer then about 1 minute. The idea cleaning if age of the cache is about 24hours is good.

My personal recommandation is to build stack of cache files. And then checking for the cache file creation/modification time or last cache file access. So clean in background (don't let become your app feel slow cause you're doing such tasks when starting or stopping an app) maybe in a thread (does iphone sdk support that? dont know :)) and check for "is cache file older then 24hrs? if yes > recache or delete file

breiti
A: 

Appreciate for your responses. I agree that checking for delete the cache at the start or quit time will reduce the program's performance. Moreover, quit time is also used for saving the program's state.

The idea to check for idle time in 1 min is quite ok, but I have to build mechanism to check idle for every 15 s during the application time. I don't think it's easy and good for performance.

At last, I decide to perform "check and delete cache" after I retrieve new Items (data + image). I will check for items (data +image) that doesn't need to be displayed anymore and delete it. I think it makes sense that the feature who saves the cache, will deletes the cache also. Certainly, I will do this in another thread to avoid frozen the interface.

Is this good? Pls give me your opinions.

Thanh-Cong Vo