Async NSURLConnection, Concurrent NSOperation, when to use NSRunLoop?

試著忘記壹切 提交于 2019-12-01 07:39:47

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.

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 ?

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!