寻找 MacOS 线程网络示例代码
我的代码需要在单独的 NSThread 中运行所有网络例程。 我有一个库,我通过一个回调例程进行通信:
my thread code
library
my callback (networking)
library
my thread code
我的回调例程必须将一些数据发布到 HTTP 服务器(NSURLConnection),等待答案(启动 NSRunLoop?),然后返回到库。
然后图书馆处理数据。 库返回到我的线程后,我可以向处理绘图和用户输入的主线程发布通知。
是否有任何示例代码介绍如何在 NSThread 中使用 NSURLConnection ?
My code needs to run all networking routines in a separate NSThread.
I have got a library, which I pass a callback routine for communication:
my thread code
library
my callback (networking)
library
my thread code
My callback routine must POST some data to an HTTP server (NSURLConnection), wait for the answer (start a NSRunLoop?), then return to the library.
The library then processes the data.
After the library returns to my thread, I can then post a notification to the main thread which handles drawing and user input.
Is there any sample code covering how to use NSURLConnection in a NSThread?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
如果您需要阻塞直到完成工作并且已经在单独的线程上,则可以使用 +[NSURLConnection sendSynchronousRequest:returningResponse:error:]。 但这有点生硬,因此如果您需要更多控制,则必须切换到异步
NSURLRequest
,并在当前NSRunLoop
中安排委托方法(即回调)。 在这种情况下,一种方法可能是让您的委托在完成时标记,并允许运行循环处理事件,直到设置标记或超过超时。If you need to block until you've done the work and you're already on a separate thread, you could use
+[NSURLConnection sendSynchronousRequest:returningResponse:error:]
. It's a bit blunt though, so if you need more control you'll have to switch to an asynchronousNSURLRequest
with delegate methods (i.e. callbacks) scheduled in the currentNSRunLoop
. In that case, one approach might be to let your delegate flag when it's done, and allow the run loop to process events until either the flag is set or a timeout is exceeded.开尔文的链接已失效,这里有一些代码可以满足您的要求(意味着在 [NSThread detachNewThreadSelector:toTarget:WithObject:] 调用的方法中运行)。 请注意,连接基本上在您进入运行循环时就开始工作,并且“terminateRunLoop”是一个 BOOL,在启动时设置为 NO,并在连接完成加载或出现错误时设置为 YES。
为什么要这样做而不是阻塞同步请求? 原因之一是您可能希望能够正确取消长时间运行的连接,即使您没有很多连接。 另外,如果您开始在主运行循环中进行大量异步请求,我还发现 UI 会有点挂起。
Kelvin's link is dead, here's a bit of code that does what you are asking for (meant to be run in a method called by [NSThread detachNewThreadSelector:toTarget:WithObject:]). Note that the connection basically starts working as soo as you enter the run loop, and that "terminateRunLoop" is meant to be a BOOL set to NO on start and set to YES when the connection finishes loading or has an error.
Why would you want to do this instead of a blocking synchronous request? One reason is that you may want to be able to cancel a long-running connection properly, even if you do not have a lot of them. Also I have seen the UI get hung up a bit if you start having a number of async requests going on in the main run loop.
回答你的小问题“启动 NSRunLoop?”:
我不确定我是否理解,但听起来你是说上面的伪代码全部在辅助线程(即不是主事件处理线程)上执行。 如果是这种情况,那么创建 NSRunLoop 可能没有任何意义,因为在等待 HTTP 服务器响应时您无法执行任何有用的工作。 就让线程阻塞吧。
To answer your mini-question "start an NSRunLoop?":
I'm not sure I understand, but it sounds like you are saying your pseudocode above is all being executed on a secondary thread (i.e., not the main event processing thread). If that's the case, there probably isn't any point in creating an NSRunLoop, because you can't do any useful work while waiting for the HTTP server to respond. Just let the thread block.
NSURLConnection 可以通过其异步委托方法在后台线程(例如 NSThread 或 NSOperation)中同步使用。 然而,它确实需要了解 NSRunLoop 的工作原理。
这里有一篇博客文章,带有示例代码和解释:
http://stackq.com/blog/?p=56
示例代码下载图像,但我已经使用这个概念对 REST API 进行复杂的 POST 调用。
-开尔文
NSURLConnection can be used synchronously, via its asynchronous delegate methods, in a background thread (e.g. NSThread or NSOperation). However, it does require knowledge of how NSRunLoop works.
There is a blog post w/ sample code and an explanation, here:
http://stackq.com/blog/?p=56
The sample code downloads an image, but I've used the concept to make sophisticated POST calls to a REST API.
-Kelvin
您使用线程有什么特殊原因吗? 除非您打开很多连接,否则主线程上的 NSRunLoop 应该足够好了。 如果您需要执行阻塞工作来响应,请创建您的线程。
Any particular reason you're using threads? Unless you're opening a lot of connections, NSRunLoop on the main thread should be good enough. If you need to do blocking work in response, create your thread then.