我想在后台线程中执行下载。为此,我创建了一个NSOperationQueue实例并向其中添加一个NSInvocationOperation实例。setDelegateQueue不能在iOS5上工作,但在iOS6上工作正常
operationQueue = [NSOperationQueue new];
// set maximum operations possible
[operationQueue setMaxConcurrentOperationCount:1];
NSInvocationOperation *operation = [[NSInvocationOperation alloc] initWithTarget:self
selector:@selector(startBackgroundThread:)
object:data];
[operationQueue addOperation:operation];
[operation release];
现在在startBackgroundThread中,我创建了一个新的NSURLConnection并调度operationQueue。
currentConnection = [[NSURLConnection alloc] initWithRequest:request delegate:self startImmediately:NO];
if (self.operationQueue) {
[currentConnection setDelegateQueue:self.operationQueue];
}
[currentConnection start];
行为iOS6的
工作iOS6的准确精细。 一个线程被创建,它在相同的后台线程上委托NSURLConnection回调。
行为的iOS5和iOS5.1
一个线程被创建,但NSURLConnection的代表并没有被调用。
我是否错过了iOS5中需要考虑的事项? 我已经阅读了苹果提供的文档,但没有提及与此相关的内容。
您要下载什么内容?它是网页内容的图像吗? – pradeepa
如果您在runloop正在运行的线程上启动连接,则不会调用委托。 – pradeepa
因此,启动运行循环始终运行的主线程上的连接 – pradeepa