NSURLConnections 是异步的多线程吗

发布于 2024-10-20 05:20:47 字数 397 浏览 1 评论 0原文

我注意到,如果我创建 NSURLConnection 并触发请求,一切都会很好。我的委托方法被调用,最后一个委托方法在调用连接的代码块完成后被调用。伟大的。

这让我相信连接是异步的,这意味着它们是多线程的。这是正确的吗?它们可以是异步的但在同一个线程中吗?不,这太疯狂了——对吧?

但是,在我见过的使用 NSOperation 的每个示例中,NSURLConnections 始终是 ScheduledInRunLoop,之后在 while 循环中调用 [runLoop runMode ...] 。

有人能准确解释这里发生了什么吗?在我看来,第一种情况需要生成辅助线程,但不需要手动调用运行循环(在这些线程上),而 NSOperation (在新线程中)确实需要手动调用运行循环。

为什么第一种情况不需要手动调用?

I've noticed that if I create an NSURLConnection and fire the request, all is well. My delegate methods get called and the last delegate method get's called well after the code block invoking the connection completes. Great.

That leads me to believe the connections are asynchronous which implies that they're multi-threaded. Is that correct? Could they be asynchronous but in the same thread? No, that's crazy - right?

But, in every example I've seen using an NSOperation, NSURLConnections are always scheduledInRunLoop after which [runLoop runMode ...] is invoked in a while loop.

Can someone explain exactly what is happening here? It seems to me that the first case requires spawning secondary threads but no manual invocation of the run loop (on those threads) while NSOperation (in a new thread) does require manual invocation of the run loop.

Why is no manual invocation required for the first case?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

浪推晚风 2024-10-27 05:20:47

NSURLConnection 确实会生成一个后台线程来管理其自身的所有实例,但这通常与调用者无关,因为委托调用是在拥有调度连接的运行循环的任何线程上进行的。(这事实上,最近事实证明与我非常相关,但这些事情实际上只有在处理多线程应用程序中的疯狂崩溃时才会出现。)

有关更多与调用者相关的详细信息,您应该查看 -[NSURLConnection ScheduleInRunLoop:forMode:]。它解释了如何手动处理调度和取消调度,以及 NSURLConnections 在多线程环境中的行为方式。

如果您不清楚运行循环如何工作以及它们如何在不需要额外线程的情况下执行异步操作,您应该阅读 运行循环。对于转向更高级的 Cocoa 开发来说,这是一个非常重要的主题。

NSURLConnection does spawn a single background thread to manage all instances of itself, but this is generally irrelevant to the caller, since the delegate calls are made on whatever thread owns the runloop the connection was scheduled in. (This fact turned out to be very relevant to me recently, but these things really only come up when dealing with insane crashers in multi-threaded apps.)

For more caller-relevant details, you should look at the docs for -[NSURLConnection scheduleInRunLoop:forMode:]. It explains how to manually handle scheduling and unscheduling, and how NSURLConnections behave in a multi-threaded environment.

If you are unclear on how run loops work and how they perform asynchronous actions without requiring additional threads, you should read Run Loops in the Threading Programming Guide. This is a very important topic for moving to more advanced Cocoa development.

夜未央樱花落 2024-10-27 05:20:47

因为主线程已经有一个运行循环,我想。

Because the main thread already has a run loop, I'd imagine.

留蓝 2024-10-27 05:20:47

如果你想在另一个线程中运行 NSURLConnection,你应该在线程的 main 方法中创建一个像这样的运行循环:

while (!finished)
{
  [[NSRunLoop currentRunLoop] runUntilDate:[NSDate dateWithTimeIntervalSinceNow:1]];
}

If you want to run NSURLConnection in another thread, you should create a run loop like this in your thread's main method:

while (!finished)
{
  [[NSRunLoop currentRunLoop] runUntilDate:[NSDate dateWithTimeIntervalSinceNow:1]];
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文