ASIHTTPRequest 如何知道 JSON 响应中有新数据

发布于 2024-12-03 14:33:32 字数 1227 浏览 2 评论 0原文

我正在开发一个 iPhone 应用程序,它使用 ASIHTTPRequest 库。我被缓存和下载数据困住了。 当我第一次打开应用程序时,它会发出请求并下载并缓存服务器的 JSON 响应。如果我对此 JSON 进行一些更改,那么内容现在会有所不同,应用程序应该会发现有新的更改,并且我的表格视图应该会上传。它不会,它会下载存储的缓存。 代码是:

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];

我正在阅读 ASIHTTPRequest 教程,它说:

“ASIHTTPRequest 可以自动将下载的数据存储在缓存中以供以后使用。这在很多情况下都会有所帮助:”

您想要仅下载自上次下载后发生更改的内容。

ASIAskServerIfModifiedCachePolicy
这与 ASIAskServerIfModifiedWhenStaleCachePolicy 相同,只不过请求将始终询问服务器更新的数据是否可用。

所以我认为我使用了正确的缓存策略,但我的应用程序没有执行我想要的操作。 我想知道他们说的是什么意思:询问服务器更新的数据是否可用??? 我如何知道是否有更新的数据?使用 Last-modified 标头响应?如果是这样,我的服务器正在正确发送 Last-modified 标头...

提前致谢!

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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

梦巷 2024-12-10 14:33:32

“询问服务器更新的数据是否可用”通常使用“If-Modified-Since”标头来完成,例如:

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

使用wireshark或charlesproxy查看发生了什么。

检查以下内容:

  1. 是否正在向服务器发送请求以检查是否应重新获取数据。

  2. 该请求具有 If-Modified-Since。

  3. 服务器正确响应 304(未修改)响应

"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

又怨 2024-12-10 14:33:32

为什么你不尊重缓存标头?这肯定是问题所在

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文