2012-07-20 151 views
1

我想使用NSURLConnection与我的服务器进行连接。我已经实现了很多次,但目前它在任何情况下都不会调用委托方法(成功/失败/超时)。我不明白为什么它会发生。NSURLConnection不调用委托方法

下面是我写的代码。

在.H我已经在.M

此获取调用,我已经通过把日志检查implenented NSURLConnectionDelegate

self.connection = nil; 
self.connection = [[NSURLConnection alloc] initWithRequest:MyRequest delegate:self startImmediately:YES]; 

我的连接对象不是零。我已经impleneted

委托方法是

- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error 
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response 
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data 
- (void)connectionDidFinishLoading:(NSURLConnection*)theConnection 

我已经在所有这些推杆日志,但他们没有获取调用。

我也检查了我的服务器,&其工作正常。 我也连接到互联网。

任何人都可以抛出一些光,我失踪了。

+0

您是否尝试过使用'connectionWithRequest:delegate:'而不是'initWithRequest:delegate:startImmediately:'? – Oyashiro 2012-07-20 14:50:03

+0

是的,我尝试使用两个,但同样的问题 – JiteshW 2012-07-21 06:19:25

+0

你可以添加更多的代码?何时以及如何创建请求? – Oyashiro 2012-07-23 07:11:31

回答

1

是的,问题得到解决。

我使用performSelectorOnMainThread在主线程上调用了我的createConnection。

1

其实有更好的解决办法,如果你正在滚动的tableview等:

NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:self startImmediately:NO]; 
[connection scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSRunLoopCommonModes]; 
[connection start]; 

当然您也可以拨打NSURLConnection的其他线程不会阻止您的连接委托回调。查看github中的示例。

相关问题