gpt4 book ai didi

iphone - NSURLCache 内存大小为零

转载 作者:行者123 更新时间:2023-12-03 19:12:58 33 4
gpt4 key购买 nike

我在使用同步调用缓存 NSURLConnection 响应时遇到问题。我在一个类中初始化缓存,然后在另一个类中使用它。请注意高速缓存容量如何初始化为 100KB,但随后又神奇地重置为零。

- (id)init {
if (self = [super init]) {
// Creates a custom URL cache that uses both memory and disk.
NSURLCache *sharedCache =
[[NSURLCache alloc] initWithMemoryCapacity:kMemoryCacheSize * 1000000
diskCapacity:kDiskCacheSize * 1000000
diskPath:diskPath];
[NSURLCache setSharedURLCache:sharedCache];

NSLog(@"Cache memory capacity = %d bytes", [[NSURLCache sharedURLCache] memoryCapacity]);
NSLog(@"Cache disk capacity = %d bytes", [[NSURLCache sharedURLCache] diskCapacity]);

//[sharedCache release];
}
return self;
}
// NSLOG OUTPUT:
// [6842:20b] Cache memory capacity = 100000 bytes
// [6842:20b] Cache disk capacity = 20000000 bytes

在另一个类(class)...

NSLog(@"Cache memory capacity = %d bytes", [[NSURLCache sharedURLCache] memoryCapacity]);
NSLog(@"Cache disk capacity = %d bytes", [[NSURLCache sharedURLCache] diskCapacity]);
NSLog(@"Cache Memory Usage = %d bytes", [[NSURLCache sharedURLCache] currentMemoryUsage]);
NSLog(@"Cache Disc Usage = %d bytes", [[NSURLCache sharedURLCache] currentDiskUsage]);
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:objectURL cachePolicy:NSURLRequestReturnCacheDataElseLoad timeoutInterval:20];
data = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
NSLog(@"Image Request finished. Error = %@",[error localizedDescription]);
NSLog(@"Cache size after = %d bytes", [[NSURLCache sharedURLCache] currentMemoryUsage]);
// NSLog Output:
// Cache memory capacity = 0 bytes
// Cache Disc Usage = 0 bytes
// Cache Memory Usage = 0 bytes
// Cache disk capacity = 20000000 bytes
// Image Request finished. Error = (null)
// Cache size after = 0 bytes

最佳答案

我在带有 WebView 的应用程序中遇到了这个问题。由于某种原因,当 webview 初始化时,它们会将缓存归零,不知道为什么。在这样的应用程序中,我使用 NSURLCache 子类,该子类会忽略对 setMemoryCapacity: 的调用(如果它们为零)。

类似于:

-(void)setMemoryCapacity:(NSUInteger)memCap
{
if (memCap == 0)
return;
[super setMemoryCapacity:memCap];
}

关于iphone - NSURLCache 内存大小为零,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2153268/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com