brucemartin
brucemartin

Reputation: 417

NSURLConnection Sticky Cache

I'm having an issue with a sticky cache when using NSURLConnection. I create a NSMutableURLRequest using the following line:

 NSMutableURLRequest *request = [[NSMutableURLRequest alloc]initWithURL:url cachePolicy:NSURLRequestReloadIgnoringLocalAndRemoteCacheData timeoutInterval:60];
[request setHTTPMethod:@"POST"];
[request setValue:@"text/javascript,application/json,application/javascript;q=0.9,*/*;q=0.8" forHTTPHeaderField:@"Accept"];
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
[request setValue:[NSString stringWithFormat:@"%d", [requestData length]] forHTTPHeaderField:@"Content-Length"];
[request setHTTPBody: requestData];

I then make the connection like so:

NSURLConnection *theConnection=[[NSURLConnection alloc] initWithRequest:request delegate:self];

On the surface everything works fine, it connects and pulls in the json object the server sends it's way. If I update the json object on the server so it contains another node and then via the App have it request the url like above, it returns a cached version of the json object. As proven by outputting the received data in

- (void)connectionDidFinishLoading:(NSURLConnection *)connection

I've tried to add a timestamp to the requested url but still received the cached version. I am resetting the receivedData var to nil before each request and reinit the var on connection:didReceiveData: but still getting the cached version.

I even set up a delegate method to force no cache:

- (NSCachedURLResponse *) connection:(NSURLConnection *)connection
               willCacheResponse:(NSCachedURLResponse *)cachedResponse{
    return nil;
}

EDIT: I verified the changed response by hitting the requested URL in a browser, but even after that the app still uses the cached data.

So my question is how can I completely destroy the cache?

Thanks, Bruce

Upvotes: 0

Views: 326

Answers (1)

brucemartin
brucemartin

Reputation: 417

I found out how to remove the cache using:

[[NSURLCache sharedURLCache] removeAllCachedResponses];

Unfortunately that still did not resolve our issue, so as the helpful person on the Apple Developers Forum suggested, we used a packet sniffer to see exactly what was being sent from the server and found out that the server was sending the incorrect info.

Thanks to all who looked into this issue.

Bruce

Upvotes: 1

Related Questions