在iOS中以编程方式清空缓存

在iOS中以编程方式清空缓存

问题描述:

有没有人巧合地知道如何清空我正在开发的iOS应用程序的缓存内存,在它进入后台的那一刻(applicationDidEnterBackground)?我已经调查了NSCache,但我仍然无法理解如何检索缓存基本上删除/释放它?

Does anyone by coincidence know how I can empty the cache memory of the iOS app that I am developing, in the moment it goes to the background (applicationDidEnterBackground)? I have investigated about NSCache but I am still not able to understand how could I retrieve the cache to basically remove/free it?

这是你在谈论的吗?

[[NSURLCache sharedURLCache] removeAllCachedResponses];

您还可以修改请求的缓存行为,以选择性地缓存响应。如果您偶然使用 AFNetworking ,则可以使用 setCacheResponseBlock 。例如。在一个项目中,我将其设置为返回所有大型视频和音频文件的 nil 。但允许它缓存较小的图像文件。

You can also modify the cache behavior of your requests to selectively cache responses. If you're using AFNetworking by chance, you can use setCacheResponseBlock. E.g. in one project I set it to return nil for all large video and audio files. But allow it to cache smaller image files.

[streamingOperation setCacheResponseBlock:^NSCachedURLResponse *(NSURLConnection *connection, NSCachedURLResponse *cachedResponse) {
    return nil; // Ensures we are not unecessarily caching asset data to Cache.db
}];