Timothy
Timothy

Reputation: 128

AFNetworking disable caching

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]

Upvotes: 2

Views: 4697

Answers (3)

Vaibhav Gautam
Vaibhav Gautam

Reputation: 2104

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

Upvotes: 0

mattt
mattt

Reputation: 19544

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).

Upvotes: 4

Ant
Ant

Reputation: 4928

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.

Upvotes: 0

Related Questions