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

Clear NSURLConnection cache

Is there a way to clear NSURLConnection cache? I used that to download some strings but I keep getting the same strings even though I changed that from my server.
moeseth
  • 1,855
  • 5
  • 23
  • 47
26
votes
3 answers

How to cache using NSURLSession and NSURLCache. Not working

I have a test app setup and it successfully downloads content from the network even if the user switches apps while a download is in progress. Great, now I have background downloads in place. Now I want to add caching. There is no point to me…
John Erck
  • 9,478
  • 8
  • 61
  • 71
24
votes
4 answers

Prevent NSURLSession from caching responses

Why does it cache responses. It returns previously fetched responses. It even works if turning off the network connection. Resetting the iOS simulator did not seem to work either. Making a request, then again with internet offline does work…
Sunkas
  • 9,542
  • 6
  • 62
  • 102
24
votes
1 answer

Transition from RestKit to pure AFNetworking 2.0

I'd been using RestKit for the last two years, but recently I've started thinking about transition from these monolith framework as it seems to be really overkill. Here's my pros for moving forward: There is big need in using NSURLSession for…
20
votes
1 answer

Is NSURLCache persistent across launches?

I'm looking for a network caching solution for my iOS application that is persistent across launches. I started to read about NSURLCache, but didn't see any mention regarding persistence. Does anyone know how this behaves when you use NSURLCache…
Brandon
  • 2,886
  • 3
  • 29
  • 44
19
votes
1 answer

How to know if a NSURLResponse comes from cache?

I set NSURLCache on a specific folder (../../Application Support/Offline so it won't be deleted randomly by the OS), then I send a NSURLRequest with NSURLRequestReturnCacheDataElseLoad policy. How can I tell that the response, handled by a class…
17
votes
3 answers

How to set the shared URLCache in swift 3?

This is the code we had in Swift 2. What is the Swift 3 version? I don't see a replacement for setShared. let sharedCache: NSURLCache = NSURLCache(memoryCapacity: 0, diskCapacity: 0, diskPath: nil) NSURLCache.setSharedURLCache(sharedCache)
Jason Hocker
  • 6,879
  • 9
  • 46
  • 79
17
votes
3 answers

NSURLCache does not clear stored responses in iOS8

Here is the sample function I call when i need to clear cache and make a new call to URL - (void)clearDataFromNSURLCache:(NSString *)urlString { NSURL *requestUrl = [NSURL URLWithString:urlString]; NSURLRequest *dataUrlRequest =…
nkp
  • 181
  • 2
  • 12
17
votes
2 answers

NSURLCache and ETags

Does NSURLCache transparently handle ETags received by server? I mean: does it automatically store ETags for each URL request and then send the appropriate If-None-Match when a request to the same URL is submitted? Or do I have to manage it by…
Sirio
  • 991
  • 2
  • 12
  • 27
14
votes
2 answers

Remove UIWebView's internal cache

I'm showing a web app in an UIWebView, and sometimes the content of pages will change. After content have been changed the app clears the cache. But when I go to a page I've previously visited the UIWebView doesn't send a HTTP GET request, but loads…
Niklas Berglund
  • 3,563
  • 4
  • 32
  • 32
13
votes
2 answers

How to use NSURLCache to return cached API responses when offline (iOS App)

I'm hoping someone can shed some light on a few things I've been researching but not making much progress on. I'd like to take advantage of NSURLCache to return cached responses for API calls that I make within an iOS App. When the device is online,…
Alfie Hanssen
  • 16,964
  • 12
  • 68
  • 74
12
votes
5 answers

On iOS where is the NSURLCache cache stored if diskPath:nil?

I've come across code that looks like this: NSURLCache *URLCache = [[NSURLCache alloc] initWithMemoryCapacity:1024 * 1024 diskCapacity:1024 * 1024 * 5 diskPath:nil]; The…
jlmendezbonini
  • 2,239
  • 21
  • 25
11
votes
3 answers

iPhone: Performances Differences Between NSURLRequestCachePolicy Settings

When using NSURLRequest on the iPhone, what are the real world performance differences between the various NSURLRequestCachePolicy settings? I am specifically interested in the cache policy's effect on the the user's perception of the speed at which…
TechZen
  • 64,370
  • 15
  • 118
  • 145
11
votes
2 answers

NSURLCache providing inconsistent results on iOS5, seemingly at random

I've just spent an entirely too long amount of time screaming my head off at NSURLCache, so I offer this bit of advice in the hope that others can avoid my misfortune. It all started off reasonably enough. My new app project only targets iOS 5 and…
Zach Lipton
  • 1,850
  • 1
  • 14
  • 28
10
votes
1 answer

How to set iOS cache and disk storage size and how does cache get restored from disk storage after app termination?

I've already asked When exactly do things get removed from urlcache's memory and disk? Now I have some more follow up questions: The memory cache is restricted by the iPhone's ram (usually 2Gb). But the disk Persistence is limited by 64Gb or…
mfaani
  • 33,269
  • 19
  • 164
  • 293
1
2 3
17 18