Async NSURLConnection, Concurrent NSOperation, when to use NSRunLoop?

后端 未结 2 1623
终归单人心
终归单人心 2021-01-14 04:08

I\'m trying to run NSURLConnection async in a secondary thread (target is iOS4), for this I have created a concurrent NSOperation, I think I\'m almost there, but am not clea

相关标签:
2条回答
  • 2021-01-14 04:23

    Well, I eventually worked this out so thought people might be interested in the details:

    1) start() was called on the main thread because I was using [NSOperationQueue mainQueue] rather than creating a new queue with [[NSOperationQueue allc] init]. However a check in NSOperation start() for whether the current thread was the mainThread or not and then calling main() directly from there or by spawning a new thread (in case we were on the mainThread) did not hurt.

    2) I realized that LinkedImageFetcher is far too complex for an understanding of run loops and threads, and unnecessarily so, because the topic is not that complicated. All that is needed within the start() method is to keep the secondary thread run loop going until we're done (isCompleted), all the run loop does for a NSURLConnection is listening to the inputs from the connection and firing callbacks (didreceivereponse, didreceivedata, etc.).

    3) yes running the run loop is necessary to get connection callbacks on that same (secondary in this case) thread.

    For more see Run Loops in Threading Programming Guide.

    0 讨论(0)
  • 2021-01-14 04:28

    I'm not sure to understand what you're trying to achieve : NSURLConnection API has a built-in async method initWithRequest:delegate:

    Is there any specific reason why you can't use this ?

    0 讨论(0)
提交回复
热议问题