When is the iPhone app cache is cleared?
Asked Answered
U

1

1

I'm working on an app that lets users record voice (among other things) to the Documents directory of the app. But when I'm recording the voice, I'm recording to the caches directory of the app and then after the user says "Okay, save this one", then I'm coping it to the Documents directory. So far all these work. But if I try to delete the data file in cache, or when I try to move it, I get problems.

So my question is, shall I just leave the data in cache so that iOS will handle it or do I need to manually delete the files in cache. If so how would I go about doing it. This is the code I have so far (which doesn't work)

    NSFileManager *fm = [NSFileManager defaultManager];
    NSString *directory = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject];
    NSError *error = nil;
    BOOL success = [fm removeItemAtPath:[NSString stringWithFormat:@"%@%@", directory, currentEntry.audioFileURL] error:&error];
    if (!success || error) {
        // it failed.
        NSLog(@"it failed to delete!!! %@ %@", error, [error userInfo]);
    } else {
        NSLog(@"Deleted... yipee... !!!");
    }
Unesco answered 23/5, 2011 at 18:4 Comment(2)
Data in $(APP_WRAPPER)/Library/Caches isn't backed up or restored, but it isn't automatically reaped, either. When it fails, what error are you getting?Centuplicate
Hi Chris, it turns out I'm using the wrong path. Managed to fix it. Thanks for the help.Unesco
R
6

I think the problem is that your path is not correct. Always use the

- (NSString *)stringByAppendingPathComponent:(NSString *)aString

method of NSString.

You can try printing out the path you get now (maybe a backslash is missing), but anyway you should use the method I described.

UPD: Another thing is that NSCachesDirectory is actually never cleaned up. Use NSTemporaryDirectory() if you want automatic cleaning.

Refreshment answered 23/5, 2011 at 18:24 Comment(1)
It seems you were right. The path that I'm setting is wrong. I managed to do it (delete the file) with the current code I have. Didn't use stringByAppendingPathComponent: for this instance. Thanks for mentioning NSTemporaryDirectory(). I will check it out. Many thanks again. CheersUnesco

© 2022 - 2024 — McMap. All rights reserved.