NSURLConnection正在返回旧数据 [英] NSURLConnection is returning old data

查看:88
本文介绍了NSURLConnection正在返回旧数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的应用程序中,我使用 NSURLConnection 方法加载JSON数据,如下所示:

In my application, I am loading JSON data using the NSURLConnection method, what looks like this:

NSURLRequest *request = [[NSURLRequest alloc] initWithURL:
                                [NSURL URLWithString:@"theurl"]];
NSURLConnection *theConnection = [[NSURLConnection alloc] initWithRequest:request
                                                                 delegate:self];
[theConnection start];

我收到并保存 -didReceiveData中的数据:方法,但是如果我在服务器上更新我的JSON文件,那么 NSURLConnection 仍会向我提供旧的JSON,同时文件正确上传到服务器。只有当我从我的iPhone(或iPhone模拟器)中删除整个应用程序并重建应用程序时,它才会收到新的Json文件。

I receive and save the data in the -didReceiveData: method, but if I update my JSON file on my server, the NSURLConnection still delivers me the old JSON, while the file is uploaded correctly to the server. Only if I delete the whole app from my iPhone (or iPhone Simulator) and rebuild the application, it receives the new Json file.

有没有人知道这个问题的解决方案?

Does anyone know a solution for this problem?

推荐答案

你需要告诉 NSURLRequest 重新评估(如果您的服务器发送适当的修改过的标头)或不使用它的缓存。这是我的 JBAsyncImageView 项目的片段(提示:使用 NSURLRequestReloadRevalidatingCacheData NSURLRequestReloadIgnoringLocalCacheData ):

You'll need to tell the NSURLRequest to re-evaluate (if your server sends the appropriate modified headers) or not use it's cache. Here's a snippet from my JBAsyncImageView project (hint: Use NSURLRequestReloadRevalidatingCacheData or NSURLRequestReloadIgnoringLocalCacheData):

// Create request
self.imageRequest = [[NSURLRequest alloc] initWithURL:imageURL 
                                          cachePolicy:(self.cachesImage) ? NSURLRequestReturnCacheDataElseLoad : NSURLRequestReloadIgnoringLocalCacheData 
                                      timeoutInterval:self.downloadTimeoutInterval];

// Begin download
self.imageData = nil;
self.imageConnection = [[NSURLConnection alloc] initWithRequest:self.imageRequest 
                                                       delegate:self 
                                               startImmediately:YES];

这篇关于NSURLConnection正在返回旧数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆