0
votes

I'm using ASIHTTPRequest API to get some JSON data from a web side. I'm using an asynchronous request without changing default cache properties. Code is as follows:

__unsafe_unretained ASIHTTPRequest * request = [ASIHTTPRequest requestWithURL:url];

request.timeOutSeconds = 30;
[request setCompletionBlock:^(void)
 {
     NSString *str = [request responseString];
     /*
     *
     *
     */
 }];
[request setFailedBlock:^(void)
 {
     NSLog(@"status: %@",[request responseStatusMessage]);
     NSLog(@"%@ : %@",[request url],[[request error] debugDescription]);         
 }];
[request startAsynchronous];

However, obtained JSON data remains as the old one although content of JSON data in server changes.

I checked data using web browser, both on laptop and on iPhone safari. When i request url after a change in JSON, it first returns old data, but if i refresh the page, it returns updated data. But in the app, ASIHTTPRequest always returns the old data.

I also try to debug ASIHTTPRequest code in order to see whether any cached data used. But it seems like it never uses download cache because it has not been set. It never enters [useDataFromCache] method.

What could be the problem? How can i force ASIHTTPRequest to check whether there is an updated data on server, and make it get the true updated JSON?

EDIT

I used Cache-Control header, and now i get the correct updated JSON data. Code is as follows:

[request addRequestHeader:@"Cache-Control" value:@"no-cache"];

However, i think from now on request will always try to retrieve JSON even if it is not modified, which will decrease performance.

How can i make it first check the server whether data is modified, and retrieve if it is modified? Currently i get JSON data as a dynamic response from a php url, so there is no file which i can check up to dateness of the data.

What could be the solution?

Regards,

2

2 Answers

1
votes

Given everything you've said, it seems unlikely that ASIHTTPRequest is cacheing the response.

So, something else must be - it seems like you have a cacheing proxy server inbetween you and the server, and that's why setting Cache-Control makes a difference.

It could be a proxy server on your local network, or it could be at your ISP, or it could be in front of the web server you're using.

0
votes

According to ASIHTTPRequest's documentation, calling the method

[[ASIDownloadCache sharedCache] clearCachedResponsesForStoragePolicy:ASICachePermanentlyCacheStoragePolicy];

will clear the cache. Call this method before you send the request and it should give you the updated JSON data.