Questions tagged [nsurlcache]

NSURLCache is a class that is used in ios developing for caching the responses of the URL. It is available in OS X v10.2 with Safari 1.0 installed, available in OS X v10.2.7 and later .

The NSURLCache class implements the caching of responses to URL load requests by mapping NSURLRequest objects to NSCachedURLResponse objects.

It provides a composite in-memory and on-disk cache, and lets you manipulate the sizes of both the in-memory and on-disk portions. You can also control the path where cache data is stored persistently.

In iOS, the on-disk cache may be purged when the system runs low on disk space, but only when your app is not running.

To create a new cache object:

   - (instancetype)initWithMemoryCapacity:(NSUInteger)memoryCapacity
                      diskCapacity:(NSUInteger)diskCapacity
                          diskPath:(NSString *)path;

Sample Example for creation of new cache:

   - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
   // Set app-wide shared cache (first number is megabyte value)
  NSUInteger cacheSizeMemory = 500*1024*1024; // 500 MB
  NSUInteger cacheSizeDisk = 500*1024*1024; // 500 MB
  NSURLCache *sharedCache = [[NSURLCache alloc] initWithMemoryCapacity:cacheSizeMemory diskCapacity:cacheSizeDisk diskPath:@"nsurlcache"];
 [NSURLCache setSharedURLCache:sharedCache];

}

Question related with this can be tagged using , or tags

Read more

267 questions
9
votes
4 answers

NSURLConnection on iOS doesn't try to cache objects larger than 50KB

Despite Apple's documentation indicating otherwise, NSURLCache on iOS doesn't do any disk (flash) caching at all. You can subclass NSURLCache to change the behaviour of the fetch and store operations to use the disk (like SDURLCache does), but due…
Ben Hoyt
  • 10,694
  • 5
  • 60
  • 84
9
votes
1 answer

NSURLCache does not work when response header value for transfer-encoding is chunked

I found an issue with (possibly) NSURLCache today while inspecting request and response headers in Charles Proxy. The issue is a little perplexing, but I'm able to repro it consistently: In a nutshell, the issue has to do with caching of…
rainypixels
  • 587
  • 4
  • 12
9
votes
6 answers

Alamofire - NSURLCache is not working?

I set my cache as below var cacheSizeMemory = 20 * 1024 * 1024 var cacheSizeDisk = 100 * 1024 * 1024 var sharedCache = NSURLCache(memoryCapacity: cacheSizeMemory, diskCapacity: cacheSizeDisk, diskPath:…
aryaxt
  • 76,198
  • 92
  • 293
  • 442
8
votes
2 answers

NSURLCache not caching

So I have subclassed NSURLCache and every time I call loadHTMLFromString: it calls storeCachedRequest:forRequest: and then cachedResponseForRequest:. Here's what I have: - (NSCachedURLResponse *)cachedResponseForRequest:(NSURLRequest *)request { …
adit
  • 32,574
  • 72
  • 229
  • 373
8
votes
2 answers

What caching algorithm does NSURLCache use?

Apple's NSURLCache class has decent documentation, but it doesn't say what caching algorithm it uses. Is it LRU, LFU, or something else entirely? I'm developing for the iPad, and am hoping to make use of NSURLCache's disk-caching abilities to cache…
Ben Hoyt
  • 10,694
  • 5
  • 60
  • 84
8
votes
2 answers

clearing a webviews cache for local files

I've found some similar posts, but they don't seem to have any effect with what I'm doing. I've got a UIWebView that I'm using to display local content in my bundle. Specifically I'm displaying a docx file. The user should rarely ever look at this…
user675446
  • 85
  • 5
8
votes
4 answers

Pre-cache Images for AFNetworking's UIImageView category

When my app loads, I pull down a JSON representation of 99 objects. Each object has an 'image_url' field, which I pass to AFNetworking's setImageWithURLRequest. My images load in a tableView, and consequently, only the first several cells make…
djibouti33
  • 12,102
  • 9
  • 83
  • 116
7
votes
1 answer

nsurlconnection asynchronous request

First of all the questions are failry simiple.. if you just want to see what they are skip to the bottom of this post and you will see them in bold.. for more detail then you can read the rest of this post... I am just trying to iron out my…
C.Johns
  • 10,185
  • 20
  • 102
  • 156
7
votes
1 answer

NSURLCache and Data Protection

I am trying to protect sensitive data stored in NSURLCache. My app's files and Core Data sqlite files are set to NSFileProtectionComplete. However, I am unable to change the NSURLCache files data protection level to anything other than…
PPierson
  • 410
  • 3
  • 18
7
votes
3 answers

NSURLCache caching random responses that should not be cached

I am implementing an app which does a lot of networking calls to a rest-api that we also control. Recently we decided to introduce caching headers on the server side to save some valuable networking and server time. As we do not know beforehand for…
Angel G. Olloqui
  • 8,045
  • 3
  • 33
  • 31
7
votes
1 answer

NSCachedURLResponse returns object, but UIWebView does not interprets content

I am sending a request to a UIWebView. There are AJAX-calls on the loaded webpage. I need to analyze the AJAX-traffic in order to determinate, if the user is logged in or not. For doing this I installed a NSURLCache in the AppDelegate: MYURLCache…
nodepond
  • 501
  • 6
  • 24
6
votes
3 answers

URLresponse is not retrieved after storing in cache using storeCachedResponse

Goal I'm trying to inject data/response from URLRequest into another URLRequest in my cache. Setup This is just a sample code. It's ready to be dumped into a project. What I'm trying to do is use the response + data retrieved from my…
mfaani
  • 33,269
  • 19
  • 164
  • 293
6
votes
2 answers

NSURLCache, together with NSURLSession, does not respect: Cache-Control: max-age:86000, private, must-revalidate

In AppDelegate.m, I configured: NSURLCache *sharedURLCache = [[NSURLCache alloc] initWithMemoryCapacity:20 * 1024 * 1024 diskCapacity:100 * 1024 * 1024 diskPath:@"FhtHttpCacheDir"]; Then the http request: - (void) testRestfulAPI{ …
foresightyj
  • 2,006
  • 2
  • 26
  • 40
6
votes
1 answer

When exactly do things get removed from urlcache's memory and disk?

let memoryCapacity = 200 * 1024 * 1024 let diskCapacity = 1 * 1024 * 1024 let cache = URLCache(memoryCapacity: memoryCapacity, diskCapacity: diskCapacity, diskPath: "myDataPath") URLCache.shared = cache Scenario1 I'm setting urlcache's memory to…
mfaani
  • 33,269
  • 19
  • 164
  • 293
6
votes
1 answer

How to clear cached data stored by URLSession/URLConfiguration?

I am using the code below to set the caching policy of my URLSession via URLConfiguration. if appDelegateObj.configuration == nil { appDelegateObj.configuration = URLSessionConfiguration.default } if self.apiType ==…
Umar Farooque
  • 2,049
  • 21
  • 32
1
2
3
17 18