1

我正在尝试在后台线程中执行下载。为此,我创建了一个 NSOperationQueue 实例并向其中添加了一个 NSInvocationOperation 实例。

        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 中考虑的内容?我已经阅读了 Apple 提供的文档,但没有提到与此相关的内容。

4

2 回答 2

1

这是 iOS 5 上的一个已知问题。请参阅http://openradar.appspot.com/10529053

于 2013-04-09T21:54:44.277 回答
0

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

[NSURLConnection sendAsynchronousRequest:队列:completionHandler:];

Block apis 提供更好的性能并被苹果推荐。

于 2013-01-15T11:47:24.937 回答