iOS, NSURLConnection: Delegate Callbacks on Different Thread?

后端 未结 4 1420
借酒劲吻你
借酒劲吻你 2020-12-28 10:20

How can I get NSURLConnection to call it\'s delegate methods from a different thread instead of the main thread. I\'m trying to mess around with the scheduleInRunLoop:forMod

4条回答
  •  清酒与你
    2020-12-28 11:14

    If you truly need to do the download in a new thread, it may be easier to detachNewThreadSelector:toTarget:withObject:, setup (and destroy) an NSAutoreleasePool, and then use one of the synchronous selectors like NSData's dataWithContentsOfURL:. This will not make use of the asynchronous NSURLConnectionDelegate.

    Because this call is synchronous, it will not return until the file has been downloaded, which will block the main thread, but because you're in a new thread, it won't. Please note that this is typically discouraged behavior. Is there other code happening in the main thread that can be optimized?

提交回复
热议问题