【发布时间】:2011-08-01 17:04:41
【问题描述】:
我这里有一些代码:
(...)
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 分钟后远程主机重置连接。
有没有人也有这个问题并且可以提供帮助?
【问题讨论】:
-
您是否尝试过从另一台服务器加载大量数据?这与特定服务器有关吗? iPhone SDK 在使用 POST 请求时的默认计时器是相当长的。无论您输入什么,它都会保持很长时间。我最终制作了自己的计时器。
标签: iphone objective-c connection nsurlconnection ios4