开发者

When is the iPhone app cache is cleared?

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 mov开发者_Go百科e 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... !!!");
    }


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜