0

I'm developing an iPhone app, that uses ASIHTTPRequest library. I''m stuck with the cache and download data. When I first open the app, it makes a request and download and cache the JSON response of the server. If I make some changes in this JSON so the content is now different, the app is supposed to catch that there's new changes and my tableview should get uploaded. It doesn't, it downloads the stored cache. The code is:

ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url2];
[[ASIDownloadCache sharedCache] setShouldRespectCacheControlHeaders:NO];
[request setDownloadCache:[ASIDownloadCache sharedCache]];
[request setCachePolicy:ASIAskServerIfModifiedCachePolicy|ASIFallbackToCacheIfLoadFailsCachePolicy];
[request setSecondsToCache:60*60*24*30]; // Cache for 30 days
[request setDidFailSelector:@selector(requestFailed:)];
[request setDidFinishSelector:@selector(requestFinished:)];

[request setDelegate:self]; // A delegate must be specified
[request startAsynchronous];

I'm reading the ASIHTTPRequest tutorial and it says:

"ASIHTTPRequest can automatically store downloaded data in a cache for use later. This can be helpful in many situations:"

You want to download something only if it has changed since you last downloaded it.

ASIAskServerIfModifiedCachePolicy
This is the same as ASIAskServerIfModifiedWhenStaleCachePolicy, except that requests will always ask the server if updated data is available.

So I think I'm using the right cache policies, but my app doesn't do what I want. I'm wondering of what they mean when they say: ask the server if updated data is available??? How can I know if there are updated data? With the Last-modified header response? If so, my server is sending the Last-modified header correctly...

Thanks in advance!

Ruben
  • 1,789
  • 2
  • 17
  • 26

2 Answers2

1

"Ask the server if updated data is available" is usually done with an 'If-Modified-Since' header, for example:

If-Modified-Since: Sat, 29 Oct 1994 19:43:31 GMT

Use wireshark or charlesproxy to see what is going on.

Check the following:

  1. That a request is getting sent to the server to check if the data should be refetched.

  2. That the request has an If-Modified-Since.

  3. That the server is correctly responding with 304 (Not Modified) response

JosephH
  • 37,173
  • 19
  • 130
  • 154
  • I have been checking it, and the server is responding correctly with the 304 response. If I don't put any cache policy, it returns 200 in the first request, and 304 in the second request. But if I inform the cache policy "ASIAskServerIfModifiedCachePolicy" it always responds 200 and downloads the json response again... I don't know what I'm doing wrong – Ruben Sep 11 '11 at 08:44
  • What does charlesproxy show as different in the request headers between the request that correctly gives a 304 and the request that should give a 304 but doesn't? – JosephH Sep 11 '11 at 09:47
0

Why are you not respecting the cache headers? That is most certainly the issue

Tony
  • 18,776
  • 31
  • 129
  • 193