NSURLCache 内存大小为零

发布于 2024-08-19 06:44:52 字数 1898 浏览 4 评论 0原文

我在使用同步调用缓存 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

在另一个班级...

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

I am having trouble caching NSURLConnection responses using a synchronous call. I initialize the cache in one class and then use it in another. Notice how the cache memory capacity gets initialized to 100KB but then is magically reset to zero later.

- (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

In another 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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

憧憬巴黎街头的黎明 2024-08-26 06:44:52

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

像这样的东西:

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

I've had this problem in apps with webviews. For some reason when webviews initialize they zero the cache, not sure why. In apps like this I use a NSURLCache subclass that ignore calls to setMemoryCapacity: if they are zero.

something like:

-(void)setMemoryCapacity:(NSUInteger)memCap
{
    if (memCap == 0)
        return;
   [super setMemoryCapacity:memCap];
}
凶凌 2024-08-26 06:44:52

我会将“%p”和 [NSURLCache sharedURLCache] 添加到您的 NSLog 语句中,以确保共享对象保持不变。然后我会为 +[NSURLCache setSharedURLCache:] 和 -[NSURLCache setMemoryCapacity:] 添加断点来查看发生了什么。

I would add "%p" and [NSURLCache sharedURLCache] to your NSLog statements to make sure the shared object remains the same. Then I would add breakpoints for +[NSURLCache setSharedURLCache:] and -[NSURLCache setMemoryCapacity:] to see what's going on.

用心笑 2024-08-26 06:44:52

这里也有同样的问题。以为缓存根本不起作用。设置一个没有 UIWebView 的小型测试项目后,[NSURLCache共享URLCache]内存容量]返回预期的10000000而不是0。

幸运的是缓存不会被清除,所有以前缓存的对象将保留。所以道格斯的解决方案很有魅力!

顺便说一句:一旦显示网页视图,大小将设置为零。之后将其设置回 10000000,显示 Web 视图不会再次将大小设置为零。

Had the same problem here. Thought caching doesn't work at all. After setting up a small test project without a UIWebView, the [NSURLCache sharedURLCache] memoryCapacity] returns the expected 10000000 instead of 0.

Luckily the cache will not be cleared, all previously cached objects will remain. So Dougs solution works like a charm!

BTW: once a web view is shown the size will be set to zero. Setting it back to 10000000 afterwards, showing a web view will not set the size to zero again.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文