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
5
votes
1 answer

NSURLCache crashes with autoreleased objects, but leaks otherwise

CSURLCache is designed to cache resources for offline browsing, as NSURLCache only stores data in-memory. If cachedResponse is autoreleased before returning the application crashes, if not, the objects are simply leaked. Any light that could be shed…
Oliver White
  • 265
  • 2
  • 9
5
votes
2 answers

Why is NSURLSession don't use my configured NSURLCache?

iOS7 brings NSURLSession, with the help of NSURLSessionConfigure, we can customize URLCache, so i tried it, but with no luck, it seems my URLCache is not used at all. - (void)testURLCache { NSURLSessionConfiguration *config =…
limboy
  • 3,879
  • 7
  • 37
  • 53
5
votes
2 answers

NSURLCache not storing cached data in iOS 7

Below given code works in iOS 6 but it does not work on iOS 7. NSCachedURLResponse cachedURLResponse = [[NSCachedURLResponse alloc] initWithResponse:response data:data userInfo:nil storagePolicy:NSURLCacheStorageAllowed]; [[NSURLCache…
Ratan
  • 1,747
  • 2
  • 18
  • 27
5
votes
1 answer

Automatically expire NSURLCache with max-age=0

I am using NSURLCache with AFNetworking. Caching works fine but there is no way to check if a response was actually retrieved from cache. To check if a cached version is available, I use [[NSURLCache sharedURLCache]…
alex
  • 4,922
  • 7
  • 37
  • 51
5
votes
0 answers

Weird WebThread Crash in iOS

I'm getting a strange crash in the webthread, happening when releasing a URLResponse. I did not directly manipulate the contents of the URLCache, so what could be the cause of this problem? Here's the crash log for reference: Incident Identifier:…
futureelite7
  • 11,462
  • 10
  • 53
  • 87
4
votes
1 answer

Large image URL response not cached by URLSession: Why?

I use a URLSession data task to download several JPG images from a backend. As the images are rather large in size (~500 KB) I want to cache the respective responses until they have expired (i.e. they have exceeded their max-age). This is the code I…
Mischa
  • 15,816
  • 8
  • 59
  • 117
4
votes
1 answer

Saving/Caching files using AFNetworking and using it for online and offline access

Our app is serving content in UIWebView which can link to PDF files, Video files, Doc files etc...We also allow user to access all of this content (Web and the linked files) in offline mode by downloading all the required files. Once all the files…
Mithin
  • 961
  • 1
  • 11
  • 37
4
votes
2 answers

NSURLCache, set custom key

I'm using NSURLCache and I'd like to set a custom key for a NSURLRequest. Is that possible? Complete explanation: I'm developing a mapping app, that uses OpenStreetMap tiles. OpenStreetMap provides multiple servers to serve the tiles, to reduce the…
Tim Autin
  • 6,043
  • 5
  • 46
  • 76
4
votes
1 answer

NSURLCache caching with different POST requests

I am using NSURLSession for networking and making POST requests to a server. I want to be able to cache these requests, however the URL is always the same. Is it possible to cache with NSURLCache and change the cache key to something unique such as…
agandi
  • 561
  • 7
  • 15
4
votes
1 answer

UIWebView and NSUrlCache

I have a NSURLCache that simply logs cachedResponseForRequest and storeCachedResponse calls. It seems that UIWebView will call cachedResponseForRequest for every request, but storeCachedResponse will only be called for the top-level page, i.e. not…
Roger
  • 563
  • 5
  • 12
4
votes
2 answers

Would a file with a response header `Cache-Control:Private` be prevented from being cached in a NSURLCache?

Would a file with a response header Cache-Control:Private be prevented from being cached in a NSURLCache? Either a shared cache (as in setSharedCache and NSURLCache.sharedCache() ) or a custom one? To expand, I have UIWebView that I need to access…
cschandler
  • 544
  • 5
  • 15
4
votes
1 answer

NSURLConnection is growing in instruments, is this NSURLCache?

So I have a memory problem with my app. The app has a MKMapView with MKOverlayRenderer which loads images on the map. Everything working ok but after 30-60 minutes the app crashes due memory. With Instruments I found that NSURLConnection is growing…
4
votes
0 answers

NSURLCache not working after iOS app termination

I am building an app in which I am implementing offline mode as well. For this I used NSURLCache mechanism. When the app is in foreground then the cache mechanism works perfectly even if the device goes in offline mode. The problem comes when I quit…
4
votes
0 answers

About Listing NSURLCache url and deleting item cache

I working on a RestFull api and i would like to test some powerful thing. To avoid always calling the server i cache some return data with NSURLRequestReturnCacheDataElseLoad. I delete my cache with [[NSURLCache sharedURLCache]…
Armanoide
  • 1,248
  • 15
  • 31
4
votes
3 answers

NSURLCache Memory Size is zero

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.…
bluefloyd8
  • 2,266
  • 2
  • 24
  • 20