我做了一个按钮来清除我的缓存:

    [[NSURLCache sharedURLCache]removeAllCachedResponses];

完成此操作后,我检查sharedURLCache的大小:
NSInteger sizeInteger = [[NSURLCache sharedURLCache] currentDiskUsage];
float sizeInMB = sizeInteger / (1024.0f * 1024.0f);

sizeInMB为0.17,有时为0.13。永不0.0。为什么removeAllCachedResponses不能将sharedURLCache设为零?

ps:在AppDelegate.m didFinishLaunchingWithOptions中:
NSURLCache *sharedCache = [[NSURLCache alloc] initWithMemoryCapacity:2 * 1024 * 1024
                                                                    diskCapacity:20 * 1024 * 1024
                                                                        diskPath:nil];

    [NSURLCache setSharedURLCache:sharedCache];

最佳答案

我已经使用NSURLCache做了小实验。下面的代码和日志:

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

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

关于ios - removeAllCachedResponses无法清除sharedURLCache吗?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/31803720/

10-09 01:31