NSURLCache Memory Size is zero

我的未来我决定 提交于 2019-12-12 09:41:43

问题


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

回答1:


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];
}



回答2:


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.




回答3:


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.



来源:https://stackoverflow.com/questions/2153268/nsurlcache-memory-size-is-zero

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!