iPhone应用程序缓存什么时候被清除?
我正在开发一个应用程序,它允许用户将语音(以及其他内容)录制到应用程序的文档目录中。但是,当我录制语音时,我会录制到应用程序的缓存目录,然后在用户说“好吧,保存这个”后,我会将其复制到文档目录。到目前为止,所有这些都有效。但是,如果我尝试删除缓存中的数据文件,或者当我尝试移动它时,就会遇到问题。
所以我的问题是,我应该将数据保留在缓存中,以便 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为问题在于你的路径不正确。始终使用
NSString
方法。您可以尝试打印出您现在获得的路径(可能缺少反斜杠),但无论如何您应该使用我描述的方法。
UPD:另一件事是 NSCachesDirectory 实际上从未被清理过。如果您想要自动清理,请使用
NSTemporaryDirectory()
。I think the problem is that your path is not correct. Always use the
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. UseNSTemporaryDirectory()
if you want automatic cleaning.