NSUrlConnectionDelegate不调用加载数据的方法

时间:2012-04-18 00:58:17

标签: ios5 asynchronous

我已经查看了NSURLConnectionDelegate connection:didReceiveData not working,但似乎没有任何好的结果,所以我很好奇为什么我无法获得任何数据。

我在didReceiveResponsedidReceiveData中添加了断点。

它打印出“连接成功”,所以我知道连接已经开始。

我正在使用ARC进行内存管理。

- (void)load {
        request = [NSMutableURLRequest requestWithURL:myURL
                                                           cachePolicy:NSURLRequestReloadIgnoringLocalCacheData
                                                       timeoutInterval:60];

    NSURLConnection *conn = [[NSURLConnection alloc] initWithRequest:request delegate:self];
    if (conn) {
        [conn start];
        NSLog(@"connection succeeded, %s", [myURL description]);  
        responseData = [NSMutableData data];
    } else {
        NSLog(@"connection failed");
    }
}

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
    responseData = [[NSMutableData alloc] init];
}

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

更新

要了解我如何测试Asynchronous unit test not being called by SenTestCase

我确实实现了jonkroll提到的两种方法,在他的回答中,我只是没有显示它们,但是,它们也没有被调用。

我之所以添加[conn start]只是因为它不起作用,我希望可以解决它,但没有这样的运气。

2 个答案:

答案 0 :(得分:3)

当你声明你的连接时:

NSURLConnection *conn = [[NSURLConnection alloc] initWithRequest:request delegate:self];

您正在创建本地指针。当您的方法完成后,由于它是NSURLConnection的最后一个强引用,ARC会释放它。您需要使用强大的ivar(和/或)属性来保存对您创建的NSURLConnection的强引用。

修改

以下是我在示例项目中测试的基本代码示例。给它一个运行。详细记录有助于。

@implementation <#Your class here#> {
    // With ARC ivars are strong by default 
    NSMutableData *_downloadedData;
    NSURLConnection *_connection;
}
-(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response{
    NSHTTPURLResponse *realResponse = (NSHTTPURLResponse *)response;
    if (realResponse.statusCode == 200){ 
        // Really any 2** but for example
        _downloadedData = [[NSMutableData alloc] init];
        NSLog(@"Good response");
    } else {
        NSLog(@"Bad response = %i",realResponse.statusCode);
    }
}
-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data{
    if (connection == _connection){
        [_downloadedData appendData:data];
        NSLog(@"Getting data...");
    }
}
-(void)connectionDidFinishLoading:(NSURLConnection *)connection{
    if (connection == _connection){
        _connection = nil;
        NSLog(@"We're done, inform the UI or the delegates");
    }
}
-(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error{
    _connection = nil;
    NSLog(@"Oh no! Error:%@",error.localizedDescription);
}
- (void)load {
    NSURL *url = [NSURL URLWithString:@"http://www.google.com/"];
    NSURLRequest *request = [NSMutableURLRequest requestWithURL:url
                                      cachePolicy:NSURLRequestReloadIgnoringLocalCacheData
                                  timeoutInterval:60];
    // Assign strong pointer to new connection
    _connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
    NSLog(@"Connection was initialized? = %@",(!!_connection)?@"YES":@"NO");
}
@end

答案 1 :(得分:0)

NSURLConnection方法initWithRequest启动对来自url的数据的异步请求。由于请求是异步完成的,因此您不能期望在调用请求的同一方法中使用响应。相反,你需要在NSURLConnection的委托回调方法中这样做。您已经实施了didReceiveResponse:didReceiveData:,但还有其他一些对您有用。

如果您想查看回复内容,请在connectionDidFinishLoading:

中查看
- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
    // response is complete, do something with the data
    NSLog(@"%@", responseData);
}

您的代码打印出“连接成功”这一事实并不意味着请求成功,只是成功创建了NSURLConnection对象。要测试连接是否存在问题,您可以实现委托方法connection:didFailWithError:

- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
    NSLog(@"Connection failed! Error - %@ %@",
          [error localizedDescription],
          [[error userInfo] objectForKey:NSURLErrorFailingURLStringErrorKey]);
}

此外,无需致电[conn start]。致电initWithRequest:

时,请求会自动启动

我建议您在Using NSURLConnection上阅读Apple的文档以获取更多详细信息。