iPhone应用程序缓存什么时候被清除?

发布于 2024-11-09 05:59:08 字数 787 浏览 5 评论 0原文

我正在开发一个应用程序,它允许用户将语音(以及其他内容)录制到应用程序的文档目录中。但是,当我录制语音时,我会录制到应用程序的缓存目录,然后在用户说“好吧,保存这个”后,我会将其复制到文档目录。到目前为止,所有这些都有效。但是,如果我尝试删除缓存中的数据文件,或者当我尝试移动它时,就会遇到问题。

所以我的问题是,我应该将数据保留在缓存中,以便 iOS 来处理它,还是需要手动删除缓存中的文件。如果是这样,我将如何去做。这是我到目前为止的代码(不起作用)

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

Spring初心 2024-11-16 05:59:08

我认为问题在于你的路径不正确。始终使用

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

NSString方法。

您可以尝试打印出您现在获得的路径(可能缺少反斜杠),但无论如何您应该使用我描述的方法。

UPD:另一件事是 NSCachesDirectory 实际上从未被清理过。如果您想要自动清理,请使用NSTemporaryDirectory()

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文