2014-01-16 55 views
0

我在NSURLConnection实例中遇到了这个问题,在使用任意数量的实例后,它们停止工作,并且从不调用它们的方法delegate。他们把工作的方法如下:mainRunloop上的NSURLConnection无法正常工作

[[NSRunLoop mainRunLoop] addPort:self.port forMode:NSDefaultRunLoopMode]; 
[_connection scheduleInRunLoop:[NSRunLoop mainRunLoop] forMode:NSDefaultRunLoopMode]; 
[self.connection start]; 
[[NSRunLoop mainRunLoop] run]; 

,他们停止使用其中一些工作后的事实,让我觉得自己跑出来的任何资源(也许在主runLoop港口或者是其他东西)。

好像一切都得到清理得很好

[_connection cancel]; 
[_connection release]; 

[[NSRunLoop mainRunLoop] removePort:self.port forMode:NSDefaultRunLoopMode]; 
[_port release];  
[super dealloc]; 

难道你们看到什么不对这种做法?任何想法为什么他们可能会停止工作

回答

1

根据Apple's documentation on NSRunLoop

运行循环模式

NSRunLoop defines the following run loop mode. 

extern NSString* const NSDefaultRunLoopMode; 
extern NSString* const NSRunLoopCommonModes; 
Constants 

NSDefaultRunLoopMode 

    The mode to deal with input sources other than NSConnection objects. 

我注意到您曾使用NSDefaultRunLoopMode什么似乎是NSConnection连接对象,虽然没有看到我真的只是猜测你的代码的其余部分。另一件你可以尝试使用的东西是performSelector:onThread:withObject:waitUntilDone:,因为你的某个连接可能会陷入竞争状态。