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
3
votes
5 answers

Alamofire unable disable caching

I can not get Alamofire or iOS to stop caching: Alamofire.SessionManager.default.session.configuration.requestCachePolicy = .reloadIgnoringLocalCacheData or URLCache.shared.removeAllCachedResponses() I need to disable it for all requests? Also…
Chris G.
  • 23,930
  • 48
  • 177
  • 302
3
votes
3 answers

What is the default diskspace that URLCache.shared instance has

Is it necessary to assign diskspace when I use URLCache.share instance? What is the default value that it has?
iOS Monster
  • 2,099
  • 1
  • 22
  • 49
3
votes
1 answer

Caching videos in ios

I have the following method playing video on AVMediaPlayerController -(void)sendRequestForVideo { NSString*VideoStr=@"http://www.ebookfrenzy.com/ios_book/movie/movie.mov"; NSURL *url = [NSURL URLWithString:VideoStr]; …
Prez
  • 227
  • 3
  • 12
3
votes
0 answers

Swift 3 / Xcode 8 - CacheRead: unable to open cache files ; NetworkStorageDB:_openDBReadConnections: failed to open read connection to DB

I am currently working on an iOS app where I need to retrieve some informations from several feeds. With these data, I create a table view to display them. Unfortunately, sometimes I get the following errors: NetworkStorageDB:_openDBReadConnections:…
Lancelot
  • 573
  • 2
  • 5
  • 27
3
votes
1 answer

NSURLSession not using cached responses

I am able to see cached responses when I query NSURLCache directly but when I request the same resource through NSURLSession:dataTaskWithRequest it always queries the server and never gives me the cached response, even with internet disabled. I…
NewShelbyWoo
  • 722
  • 1
  • 7
  • 21
3
votes
1 answer

How to disable the URLCache completely with Alamofire

Using Xcode 7.3 iOS 9.2 all in Swift none of that horrible Objective C I have been working on this for a while and been around the block three times now. I have tried this post here and it didn't…
MNM
  • 2,673
  • 6
  • 38
  • 73
3
votes
0 answers

cachedResponseForRequest not working on ios 9.3

I have an application which was working till ios 9.2 and now it suddenly stopped working since ios 9.3. Issue is: cachedResponseForRequest is giving us values null if we do the following in webViewDidFinishLoad. func webViewDidFinishLoad(webView:…
lifemoveson
  • 1,661
  • 8
  • 28
  • 55
3
votes
1 answer

NSURLCache disregarding max-age

I'm using the following code to download some data from TfL. NSURLSession *session = [NSURLSession sessionWithConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration] delegate:self…
Jonathan.
  • 53,997
  • 54
  • 186
  • 290
3
votes
1 answer

iOS: AFNetworking's AFHTTPSessionManager cache policy

I have a standard subclass of AFHTTPSessionManager. I am using the initWithBaseURL:sessionConfiguration: initializer by providing the session configuration I need. I am setting the requestCachePolicy variable on the configuration with the intention…
Petar
  • 2,241
  • 1
  • 24
  • 38
3
votes
2 answers

How to use NSURLSession to determine if resource has changed?

I'm using NSURLSession to request a JSON resource from an HTTP server. The server uses Cache-Control to limit the time the resource is cached on clients. This works great, but I'd also like to cache a deserialized JSON object in memory as it is…
Doug Richardson
  • 10,483
  • 6
  • 51
  • 77
3
votes
0 answers

Using NSURLCache to display data quickly, update in background

I'd like my iOS application (at least certain endpoints) to have the following network behavior: Always use the cache, whenever it's available, no matter the age (draw the UI right away) If the data is stale, also make a network request (the UI has…
SimplGy
  • 20,079
  • 15
  • 107
  • 144
3
votes
1 answer

How to use NSURLCache?

I'm making application using VK iOS SDK (russian social network). I'm sending request for getting some data from wall. I want to caching that data, but I don't really understand what should I do next. In AppDelegate I'm make: -…
mr.gray
  • 53
  • 6
3
votes
1 answer

Memory Leak when using SDURLCache (subclass of NSURLCache)

I am using Olivier Poitrey's SDURLCache (github link) as an alternative to NSURLCache to enable disk-caching in an iPhone app. It works very well but is curiously leaking a NSHTTPURLResponseInternal when a disk cached object is returned (no leak…
prendio2
  • 1,885
  • 17
  • 25
3
votes
1 answer

iOS - Is it possible to cache the chunked HTTP response?

I'm getting data from backend using AFNetworking and set request's cachePolicy as NSURLRequestUseProtocolCachePolicy. The response headers contain ETag value and Transfer-Encoding is chunked. In the second time I call the same API, it gets the…
Hai Hw
  • 1,397
  • 1
  • 16
  • 24
3
votes
3 answers

NSURLCache - Disk caching for GET request with parameters not working

I'm trying to cache some HTTP requests to disk using NSURLCache. After doing some tests with AFNetworking and not working, I created a simple example using NSURLRequest. I initialize the cache on the AppDelegate like this, setting the memory size to…
jcardenete
  • 1,646
  • 3
  • 13
  • 16