2013-01-15 180 views
1

我想在后台线程中执行下载。为此,我创建了一个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中需要考虑的事项? 我已经阅读了苹果提供的文档,但没有提及与此相关的内容。

+0

您要下载什么内容?它是网页内容的图像吗? – pradeepa

+0

如果您在runloop正在运行的线程上启动连接,则不会调用委托。 – pradeepa

+0

因此,启动运行循环始终运行的主线程上的连接 – pradeepa

回答

0

如果您尝试执行后台Web请求,请使用以下便捷方法。

 
[NSURLConnection sendAsynchronousRequest: queue: completionHandler:]; 

块apis给予更好的性能,并由苹果推荐。

+0

谢谢....此解决方案在iOS5以及iOS6中工作。但仍然处于困境中,为什么setDelegateQueue不能在iOS5上工作,而在iOS6上工作正常 – Apoorv

相关问题