When is the iPhone app cache is cleared?

我只是一个虾纸丫 提交于 2019-12-11 07:45:31

问题


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... !!!");
    }

回答1:


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.



来源:https://stackoverflow.com/questions/6101067/when-is-the-iphone-app-cache-is-cleared

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!