NSURLCache memory size is zero

I'm 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 capacity is initialized to 100 KB, but then 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 
+4
source share
3 answers

I had this problem in webview apps. For some reason, when web views are initialized, they zero out the cache, not knowing why. In such applications, I use a subclass of NSURLCache that ignores calls to setMemoryCapacity: if they are zero.

sort of:

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

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 find out what is happening.

+1
source

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

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

BTW: after the web view is displayed, the size will be set to zero. After returning to 10,000,000, displaying the web view will not resize to zero.

0
source

Source: https://habr.com/ru/post/1299630/


All Articles