本文介绍了removeAllCachedResponses无法清除sharedURLCache?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个按钮来清除缓存:

I made a button to clear my cache:

    [[NSURLCache sharedURLCache]removeAllCachedResponses];

完成后,检查sharedURLCache的大小:

after this being done, I check the size of sharedURLCache:

NSInteger sizeInteger = [[NSURLCache sharedURLCache] currentDiskUsage];
float sizeInMB = sizeInteger / (1024.0f * 1024.0f);

sizeInMB为0.17,有时为0.13。从不0.0。为什么removeAllCachedResponses不会使sharedURLCache为ZERO?

the sizeInMB is 0.17, sometimes 0.13. Never 0.0. Why the removeAllCachedResponses doesn't make the sharedURLCache to ZERO ?

ps:在AppDelegate.m didFinishLaunchingWithOptions:

ps: in AppDelegate.m didFinishLaunchingWithOptions:

NSURLCache *sharedCache = [[NSURLCache alloc] initWithMemoryCapacity:2 * 1024 * 1024
                                                                    diskCapacity:20 * 1024 * 1024
                                                                        diskPath:nil];

    [NSURLCache setSharedURLCache:sharedCache];


推荐答案

我做了NSURLCache的小实验。代码和日志如下:

I've done small experiment with NSURLCache. Code and logs below:

NSUInteger memorySize = 1024 * 1024 * 64;
NSURLCache *sharedCache = [[NSURLCache alloc] initWithMemoryCapacity:memorySize diskCapacity:memorySize diskPath:nil];
[NSURLCache setSharedURLCache:sharedCache];
NSLog(@"cap: %ld", [[NSURLCache sharedURLCache] diskCapacity]);

NSInteger sizeInteger = [[NSURLCache sharedURLCache] currentDiskUsage];
float sizeInMB = sizeInteger / (1024.0f * 1024.0f);
NSLog(@"size: %ld,  %f", (long)sizeInteger, sizeInMB);

[[NSURLCache sharedURLCache] removeAllCachedResponses];

sizeInteger = [[NSURLCache sharedURLCache] currentDiskUsage];
sizeInMB = sizeInteger / (1024.0f * 1024.0f);
NSLog(@"size: %ld,  %f", (long)sizeInteger, sizeInMB);

[[NSURLCache sharedURLCache] removeAllCachedResponses];

sizeInteger = [[NSURLCache sharedURLCache] currentDiskUsage];
sizeInMB = sizeInteger / (1024.0f * 1024.0f);
NSLog(@"size: %ld,  %f", (long)sizeInteger, sizeInMB);

日志:

2015-08-05 11:26:29.901 lagoon[26845:533709] cap: 67108864
2015-08-05 11:26:29.904 lagoon[26845:533709] size: 86016,  0.082031
2015-08-05 11:26:29.907 lagoon[26845:533709] size: 123128,  0.117424
2015-08-05 11:26:29.910 lagoon[26845:533709] size: 123128,  0.117424

这是全新安装的应用程序,没有任何网络请求,已经占用了一些空间,你可以看到日志。所以我认为它在缓存中有一些额外的开销,它与真实的网络数据无关。

It's completely new installation of app without any network requests and there is already some space occupied as you can see in logs. So I think it has some additional overhead in the cache and it's not related to real network data.

这篇关于removeAllCachedResponses无法清除sharedURLCache?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

11-02 16:31