2

I'm trying to get a JSON file from a server then display it in a table, this works fine, however, for some reason AFNetworking is caching the JSON file even after a app restart. How can I disable this?

 NSURL *url = [NSURL URLWithString:@"http://?json"];

    NSURLRequest *request = [NSURLRequest requestWithURL:url];

    AFJSONRequestOperation *operation = [AFJSONRequestOperation
                                         JSONRequestOperationWithRequest:request
                                         success:^(NSURLRequest *request, NSHTTPURLResponse *response, id responseObject)
                                         {

                                             self.dataget = [responseObject objectForKey:@"data"];
                                             [self.tableView reloadData];


                                         }
                                         failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, id responseObject)
                                         {

                                             [HUD hideUIBlockingIndicator];
                                         }];

    [operation start];

The json file is probably not cached server side: Cache-Control: no-cache[CRLF]

Timothy
  • 128
  • 1
  • 12
  • possible duplicate of [How To Disable AFNetworking Cache](http://stackoverflow.com/questions/9968050/how-to-disable-afnetworking-cache) – UpL1nK Aug 08 '13 at 14:02

3 Answers3

4

Cache behavior can be set on NSMutableURLRequest objects, with setCachePolicy:. Otherwise, the built-in shared NSURLCache will respect the caching behavior defined by the server (which I would recommend tuning and taking advantage of, rather than outright disregarding).

mattt
  • 19,544
  • 7
  • 73
  • 84
  • Stupid me, I did set an NSURLCache following a tutorial to `NSURLCache *URLCache = [[NSURLCache alloc] initWithMemoryCapacity:4 * 1024 * 1024 diskCapacity:20 * 1024 * 1024 diskPath:nil]; [NSURLCache setSharedURLCache:URLCache];` Removed that from the AppDelegate and works fine now. – Timothy Aug 09 '13 at 10:15
  • I cover this info & more in the following blog post : http://blog.originate.com/blog/2014/02/20/afimagecache-vs-nsurlcache/ – vfxdrummer Mar 14 '14 at 18:46
0

AFNetworking doesn't do any caching. Also, the "Cache-Control" HTTP header tells the client not to cache the page (ie. AFNetworking), not the server.

It sounds like your server is caching the JSON page.

Ant
  • 4,890
  • 1
  • 31
  • 42
  • But, if this is the case, why does my browser show the most recent version of the JSON and the app doesn't? – Timothy Aug 08 '13 at 13:28
  • Hmm, interesting. AFNetworking uses the built-in NSURLCache, so I was wrong on that. What do you get if you visit the page via the browser on the iOS device/simulator? Is the device configured to access the server in the same way as the browser (ie. no proxies in between)? – Ant Aug 08 '13 at 15:47
0

AFNetworking doesn't cache anything. You should probably check the cache control headers of the response. If i am not wrong then your server is sending some cache control headers which NSUrlConnection is taking into consideration. I would recommend you to set the caching policy of NSURLRequest to NSURLRequestReloadIgnoringLocalCacheData before making request to server

Vaibhav Gautam
  • 2,074
  • 17
  • 17