在iOS中以编程方式清空缓存 [英] Empty the cache programmatically in iOS

查看:129
本文介绍了在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
}];

这篇关于在iOS中以编程方式清空缓存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆