【发布时间】:2017-04-07 17:26:05
【问题描述】:
首先,我不认为这个问题已经存在,但我同意有类似的帖子,请继续阅读。
我的问题是:如何中止处于“正在连接”状态的 NSUrlConnection?我的意思是,建立连接后,我们可以使用NSUrlConnection cancel 方法取消请求。但是,在服务器没有提供响应(在接收任何委托调用之前)达到超时之前,如何在“连接”状态中中止它?
感谢您的宝贵时间!
编辑
我应该使用NSURLSessionTask 而不是NSUrlConnection 来做到这一点(使用它的方法cancel)?
编辑 2 - 代码示例
NSURLConnection* m_connection;
m_connection = [[NSURLConnection alloc] initWithRequest:request delegate:self startImmediately:NO];
if(m_connection){
[m_connection start];
m_timer = [NSTimer scheduledTimerWithTimeInterval: FLT_MAX
target: self selector: @selector(doNothing:)
userInfo: nil repeats:YES];
m_runLoop = [NSRunLoop currentRunLoop];
[m_runLoop addTimer:m_timer forMode:NSDefaultRunLoopMode];
while (m_bRunLoop && [m_runLoop runMode:NSDefaultRunLoopMode beforeDate:[NSDate distantFuture]]);
[m_connection cancel];
}
我使用我的连接来传输数据。如您所见,现在我将连接设置 m_bRunLoop 中止到 false 并且没关系。但我的问题是:如何在服务器发送响应之前中止我的连接,而不等待整个超时?
【问题讨论】:
-
你能把你创建
NSURLConnection实例的代码贴出来,你想取消它们的地方,是在同一个类还是不同类? -
我编辑了我的帖子,不同的班级或相同的没关系,我可以处理。
标签: ios nsurlconnection