我这里有一些代码:
(...)
NSURLConnection *theConnection = [[NSURLConnection alloc]
initWithRequest:theRequest delegate:self];
if( theConnection )
{
webData = [[NSMutableData data] retain];
}
和委托方法:
-(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
NSLog(@"zero");
[webData setLength: 0];
}
-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
NSLog(@"got %d", [data length]);
[webData appendData:data];
}
-(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
NSLog(@"ERROR with theConenction");
[connection release];
[webData release];
}
-(void)connectionDidFinishLoading:(NSURLConnection *)connection
{
NSLog(@"DONE. Received Bytes: %d", [webData length]);
[connection release];
[webData release];
}
在有更多数据出现之前,它的效果非常好。比 NSURLConnection “停止工作”(没有错误,没有异常,没有退出)。我仍然可以使用我的应用程序。我注意到didReceiveData
在连接期间被多次调用时问题开始了。
从调试器一步一步看起来如何:
1. I call theRequest
2. Delegate call didReceiveResponse
2010-06-21 18:10:16.708 MyApp[9477:207] 零
3. Delegate call didReceiveData
2010-06-21 18:10:16.709 MyApp[9477:207] 得到 6912
(gdb) 继续
4. Delegate call didReceiveData (once again)
2010-06-21 18:10:18.027 MyApp[9477:207] 得到 114067
(gdb) 继续
--> and here is the problem <--
主循环继续没有断点,并且connectionDidFinishLoading
没有被调用。当 didRecieveData 只被调用一次时,一切正常。
5. Delegate call didFailWithError (after 5 min!)
2010-06-21 18:15:18.041 MyApp [9477:207] 连接错误,连接失败!错误 - 操作无法完成。对等方重置连接
(gdb) 继续
=============== 更新 ====================
最后,我发现了一件重要的事情:真正的问题是远程主机有时无法以正确的方式完成连接(即大量数据),因此无法调用委托 connectionDidFinishLoading 并且在 5 分钟后远程主机重置连接。
有没有人也有问题并且可以提供帮助?