问题描述:

I'm upgrading my iPhone application, which has the content downloading function using Http communication.

The event used to occur in the main thread and the NSURLConnectionDelegate functions used to be called in the other thread on iOS 4.

But when I tested the same code on iOS 5, I found that the NSURLConnectionDelegate functions were called in the main thread.

I did not use the NSURLConnection sendSynchronousRequest:, but the initWithRequest:.

Did Apple upgrade this part of NSURLConnection in iOS 5?

网友答案:

NSURLConnection has a few new methods in iOS 5 e.g. sendAsynchronousRequest: ..., so yes they updated it in some way.

To fix it you could use

- (void)performSelector:(SEL)aSelector onThread:(NSThread *)thr withObject:(id)arg waitUntilDone:(BOOL)wait

in your delegate method or simply dispatch on the right queue.

The docs state:

If you don’t schedule the connection in a run loop or an operation queue before calling this method, the connection is scheduled in the current run loop in the default mode.

On what thread did you -start your connection?

Try to wrap your [connection startImmediately:YES] in a dispatch_async(myThreadQueue ...) call.

网友答案:

If you pass YES to startImmediately, then the delegate methods are called on the current run loop. If you do this from the main thread, then that's the main run loop.

However, if you simply pass NO and manually start the request, then the delegate methods will be run on a different thread (not the main thread).

相关阅读:
Top