NSURLConnection委托在15次快速调用后只获取一次connectionDidFinishLoading

时间:2014-07-30 13:26:58

标签: ios objective-c cocoa-touch

我使用NSURLConnection

创建了15个快速请求
NSURLConnection *conn = [[NSURLConnection alloc] initWithRequest:url delegate:callback];
[conn start];

服务器接收所有15个请求并提供适当的响应。但我的委托对象只接收一个connectionDidFinishLoading:方法调用。

可能是什么原因?

1 个答案:

答案 0 :(得分:1)

尝试在主运行循环上运行NSURLConnection:

NSURLConnection * connection = [[NSURLConnection alloc] 
                            initWithRequest:request
                                   delegate:callback 
                           startImmediately:NO];

[connection scheduleInRunLoop:[NSRunLoop mainRunLoop] 
                      forMode:NSDefaultRunLoopMode];
[connection start];

此外,使用时:

 [[NSURLConnection alloc] initWithRequest:url delegate:callback];

由于连接已经启动,您无需致电start。只有在使用start并将initWithRequest:delegate:startImmediately:传递给NO时才需要startImmediately