What does housekeeping for [NSURLCache sharedURLCache]?

This code is from Apple’s sample code, URL CacheInfo.

Interestingly, the [NSURLCahce sharedURLCache] keeps track of cached data a given NSURLRequest.

There is no code which ask to keep track of cache for the request, but Cocoa seems to keep the cache automatically and returns cached request when [sharedCache cachedResponseForRequest:theRequest] is called.
Its delegate class receives cached data for the given request when NSURLConnection is created and invoked.

What I expect how it works is to retrieve cached data by asking to some object, but instead it requires cached request and the next step is same to that of first request of the URL data. In other words, receiving cached data is same to receiving data from a given web site pointed by its URL.

It looks very clever design of the framework!

But what does housekeeping? Is it Objective-C runtime? Oh… the OS itself, right? So, OS knows [NSURLCahce sharedURLCache] and probably allocates space for it. Then when cachedResponseForRequest: called, the method fetches data in the space.

- (void) createConnectionWithPath:(NSString *)thePath
    /* Creates the URL request. The cache policy specifies that the
     existing cached data should be used to satisfy the request,
     regardless of its age or expiration date. */

    NSURLRequest *theRequest = [NSURLRequest requestWithURL:[NSURL URLWithString:thePath]
						cachePolicy:NSURLRequestReturnCacheDataElseLoad timeoutInterval:60];

	/* Finds out if the request response is found in the shared cache.
	 If so, displays the elapsed time in gray to indicate a cache hit. */

    NSURLCache *sharedCache = [NSURLCache sharedURLCache];
	NSCachedURLResponse *response = [sharedCache cachedResponseForRequest:theRequest];
	if (response) {
		[timeField setTextColor:[NSColor grayColor]];
	else {
		[self reset];
		[self controlsEnabled:NO];
		[timeField setTextColor:[NSColor blackColor]];

	/* Creates the URL connection with the request and starts loading the
	 data. */

    self.connection = [[NSURLConnection alloc] initWithRequest:theRequest
    					delegate:self startImmediately:YES];

    if (self.connection) {
		/* record the start time of the connection */
		self.connectionTime = [NSDate date];

		/* create an object to hold the received data */
		self.receivedData = [NSMutableData data];

		/* show the user that loading activity has begun */
		[self.progressIndicator startAnimation:self];


One response to this post.

  1. Posted by alex on April 10, 2012 at 9:56 PM

    this is only for arc right? :(


Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: