NSURLConnection不刷新

时间:2011-06-20 14:19:51

标签: iphone asynchronous sdk connection nsurlconnection

我有一个NSURLConnection,我每次调用-(void)viewWillAppear:animated时都会调用它(这只是暂时的,它仅用于测试)

我这样做

receivedData = [[NSMutableData data] retain];
NSString *urlString = @"<URL hidden>";
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:urlString] cachePolicy:NSURLRequestReturnCacheDataElseLoad timeoutInterval:30.0];
[NSURLConnection connectionWithRequest:request delegate:self];

然后我有这三个委托方法:

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
    [receivedData appendData:data];
}

- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
    NSString *returnString = [[NSString alloc] initWithData:receivedData encoding:NSUTF8StringEncoding];
    usersPhotos = [[NSMutableArray alloc] initWithArray:[[returnString JSONValue] objectForKey:@"data"]];
    [self loadAnnotations];

    NSLog(@"%@", returnString);

    [returnString release];
}

- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
    [self loadAnnotations];
}

即使我确定returnString应该更改(我可以访问urlString中的网站并确认它已更改),但它始终是相同的。

就好像它重用了从第一个连接中检索的数据。

有谁知道这是为什么?

2 个答案:

答案 0 :(得分:6)

它应该是您的缓存策略,我将在一秒钟内使用正确的信息进行更新。


<强>更新

尝试将您的cahce政策设置为:cachePolicy:NSURLRequestReloadIgnoringCacheData

应该是:

NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:urlString] cachePolicy:NSURLRequestReloadIgnoringCacheData timeoutInterval:30.0];

答案 1 :(得分:0)

如果您正在重复使用它,则需要清空缓冲区。

添加此方法:

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{   
    [receivedData setLength:0];
}