views:

293

answers:

4

I cache the images to the document directory of my app using the following code.

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *saveDirectory = [paths objectAtIndex:0];

But even after I close the application, its still there. How can I clear it when I lose my app. I am doing all this in my simulator.

+1  A: 

If you do not want your cached images to be preserved after application is closed better save them to temporary directory - they will be removed automatically.

If you want to manually remove the files you must store the paths for them and use the following NSFileManager function:

- (BOOL)removeItemAtPath:(NSString *)path error:(NSError **)error

Edit: sorry, I appeared to be wrong here about automatic deleting. Here's a quote from Developing Guide:
Use this directory to write temporary files that you do not need to persist between launches of your application. Your application should remove files from this directory when it determines they are no longer needed. (The system may also purge lingering files from this directory when your application is not running.)

Vladimir
But how to get my file to save to temporary Directory?
wolverine
NSString *saveDirectory = NSTemporaryDirectory(); and then save your file there as you do for any other path.
Vladimir
I have been trying this until now. But its not getting deleted when i close the app. Its still present there.
wolverine
My guess is that they will be deleted when the OS need more space in there (or after you reboot your phone). It just means that you can disregard them, if you want to make sure that they are actually removed you need to do something similar as suggested in other replies.
m5h
The simulator will never delete files in the temporary path because it does not need to. The device will, eventually, if it is needed.
Kendall Helmstetter Gelner
+1  A: 
NSString *file;
NSDirectoryEnumerator *dirEnum = [[NSFileManager defaultManager] enumeratorAtPath:saveDirectory];
NSError* err;
while (file = [dirEnum nextObject]) {
    err = nil;
    [[NSFileManager defaultManager] removeItemAtPath:[saveDirectory stringByAppendingPathComponent:file] error:&err]];
    if(err){
        //print some errror message
    }
}
Morion
Good one. Will try this one too.
wolverine
+2  A: 

I would implement applicationWillTerminate: in my application delegate and remove the cache files there. Or better yet, as suggested by Vladimir, save them in a temporary directory and let the OS clean them up when needed.

- (void)applicationWillTerminate:(UIApplication *)app
{
    NSFileManager *fileManager = [NSFileManager defaultManager];
    NSError *error;
    NSArray *cacheFiles = [fileManager contentsOfDirectoryAtPath:saveDirectory error:error];
    for (NSString *file in cacheFiles) {
        error = nil;
        [fileManager removeItemAtPath:[saveDirectory stringByAppendingPathComponent:file] error:error];
        /* handle error */
    }
}
m5h
By the temporary directory do u mean the tmp present in the application support folder for the app? If then, how can I access it? NSDcocumentDirectory will get the documents folder.
wolverine
I meant the NSTemporaryDirectory() as suggested above. Replied there with regards to your question about why things weren't removed.
m5h
+1  A: 

Use the temporary directory path as specified in this question:

http://stackoverflow.com/questions/1567134/how-can-i-get-a-writable-path-on-the-iphone

Kendall Helmstetter Gelner