5

In my application, I am loading JSON data using the NSURLConnection method, what looks like this:

NSURLRequest *request = [[NSURLRequest alloc] initWithURL:
                                [NSURL URLWithString:@"theurl"]];
NSURLConnection *theConnection = [[NSURLConnection alloc] initWithRequest:request
                                                                 delegate:self];
[theConnection start];

I receive and save the data in the -didReceiveData: method, but if I update my JSON file on my server, the NSURLConnection still delivers me the old JSON, while the file is uploaded correctly to the server. Only if I delete the whole app from my iPhone (or iPhone Simulator) and rebuild the application, it receives the new Json file.

Does anyone know a solution for this problem?

iHunter
  • 6,205
  • 3
  • 38
  • 56
Jelle
  • 284
  • 3
  • 13
  • Sounds like the result is being cached, see [this answer](http://stackoverflow.com/questions/405151/is-it-possible-to-prevent-an-nsurlrequest-from-caching-data-or-remove-cached-dat) – Douglas Jan 01 '12 at 19:37

2 Answers2

9

You'll need to tell the NSURLRequest to re-evaluate (if your server sends the appropriate modified headers) or not use it's cache. Here's a snippet from my JBAsyncImageView project (hint: Use NSURLRequestReloadRevalidatingCacheData or NSURLRequestReloadIgnoringLocalCacheData):

// Create request
self.imageRequest = [[NSURLRequest alloc] initWithURL:imageURL 
                                          cachePolicy:(self.cachesImage) ? NSURLRequestReturnCacheDataElseLoad : NSURLRequestReloadIgnoringLocalCacheData 
                                      timeoutInterval:self.downloadTimeoutInterval];

// Begin download
self.imageData = nil;
self.imageConnection = [[NSURLConnection alloc] initWithRequest:self.imageRequest 
                                                       delegate:self 
                                               startImmediately:YES];
Jesse Bunch
  • 6,651
  • 4
  • 36
  • 59
  • I ran into this problem when iOS 7 was rolled out. The solution above did NOT fix my problem. After trying a couple different things I had to use the solution below (adding timestamp to request). – Mr. Spock Nov 13 '13 at 15:13
  • the documentation says that "NSURLRequestReloadIgnoringLocalCacheData" is not implemented? – Clement Prem Mar 10 '15 at 13:10
5

The easiest solution to prevent request caching is to add a timestamp parameter with current time at the end:

http://example.com/api.json?my=args&timestamp=2344992923
iHunter
  • 6,205
  • 3
  • 38
  • 56
  • 1
    Actually, it's probably easier to just tell `NSURLRequest` to re-evaluate or not cache the data---rather than hacking the HTTP request. For AJAX requests, this would be the easiest way. On iOS, however, not so much. – Jesse Bunch Jan 01 '12 at 20:01
  • 1
    @JesseBunch Timestamp method has an advantage in case of badly configured server-side or proxy caching. – iHunter Jan 01 '12 at 20:57
  • True, but if you tell it not to use the cache you don't have to worry about it. And for what it's worth, I didn't downvote you. – Jesse Bunch Jan 01 '12 at 20:58
  • 1
    @JesseBunch What if server or proxy caches that response? You'll send a request ignoring local cache but will receive a cached response anyway. I'm not arguing my technique is better then yours (I prefer your one, it's much clearer then mine), but in some rare cases the hackish way may be of use. – iHunter Jan 01 '12 at 21:06
  • You're right. In that case, your solution may be the only way to do it. – Jesse Bunch Jan 01 '12 at 21:12