NSURLConnection正在返回旧数据

Jel*_*lle 5 nsurlconnection ios

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

NSURLRequest *request = [[NSURLRequest alloc] initWithURL:
                                [NSURL URLWithString:@"theurl"]];
NSURLConnection *theConnection = [[NSURLConnection alloc] initWithRequest:request
                                                                 delegate:self];
[theConnection start];
Run Code Online (Sandbox Code Playgroud)

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

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

Jes*_*nch 9

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

// 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];
Run Code Online (Sandbox Code Playgroud)


iHu*_*ter 5

防止请求缓存的最简单方法是在最后添加一个带有当前时间的timestamp参数:

http://example.com/api.json?my=args&timestamp=2344992923
Run Code Online (Sandbox Code Playgroud)