【发布时间】:2011-06-05 03:28:16
【问题描述】:
我正在尝试让多个 NSURLConnections 并行(同步)运行,但是如果它不在主线程上运行(代码块在下面注释掉),则 URL 连接似乎根本不起作用(无的 NSURLConnection 委托方法被触发)。这是我的代码(NSOperation 子类的实现文件):
- (void)start
{
NSLog(@"DataRetriever.m start");
if ([self.DRDelegate respondsToSelector:@selector(dataRetrieverBeganExecuting:)])
[self.DRDelegate dataRetrieverBeganExecuting:identifier];
if ([self isCancelled]) {
[self finish];
} else {
/*
//If this block is not commented out NSURLConnection works, but not otherwise
if (![NSThread isMainThread])
{
[self performSelectorOnMainThread:@selector(start) withObject:nil waitUntilDone:NO];
return;
}*/
SJLog(@"operation for <%@> started.", _url);
[self willChangeValueForKey:@"isExecuting"];
_isExecuting = YES;
[self didChangeValueForKey:@"isExecuting"];
NSURLRequest * request = [NSURLRequest requestWithURL:_url];
_connection = [[NSURLConnection alloc] initWithRequest:request
delegate:self];
if (_connection == nil)
[self finish];
} //not cancelled
}//start
用调试器运行它,在这个 start 方法结束后,没有一个 NSURLConnection 委托触发(我在那里设置了断点)。但是在主线程上它工作得很好。有什么想法吗?谢谢!
【问题讨论】:
标签: objective-c cocoa-touch ios4 nsurlconnection nsurlrequest