Use NSURLConnection cache when device is disconnected

In the NSOperation subclass, I use the following code to download the xml file from our server and then parse it:

NSURLRequest * request = [NSURLRequest requestWithURL:[NSURL URLWithString:url] cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:15]; NSData * receivedData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil]; 

The second time I make the same request, the server returns HTTP 304, and the cached response data is stored in receivedData . So far so good.

My question is: can I get the same cache response when the device is disconnected?

+4
source share
1 answer

NSURLCache does not support disk caching. You can simply save it manually or use it: https://github.com/steipete/SDURLCache

This is a very simple cache that does its job ... very simple use, only one class ... It also supports etags.

 // Do this only once in your app... SDURLCache *urlCache = [[SDURLCache alloc] initWithMemoryCapacity:1024*1024 // 1MB mem cache diskCapacity:1024*1024*5 // 5MB disk cache diskPath:[SDURLCache defaultCachePath]]; [NSURLCache setSharedURLCache:urlCache]; [urlCache release];
// Do this only once in your app... SDURLCache *urlCache = [[SDURLCache alloc] initWithMemoryCapacity:1024*1024 // 1MB mem cache diskCapacity:1024*1024*5 // 5MB disk cache diskPath:[SDURLCache defaultCachePath]]; [NSURLCache setSharedURLCache:urlCache]; [urlCache release]; 
0
source

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


All Articles