ASIHTTPRequest seems to cache JSON data always

心不动则不痛 提交于 2019-12-13 04:13:56

问题


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,


回答1:


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.




回答2:


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.



来源:https://stackoverflow.com/questions/9251867/asihttprequest-seems-to-cache-json-data-always

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!