NSURLConnection阻止主线程吗? [英] Does NSURLConnection block the main thread?

查看:96
本文介绍了NSURLConnection阻止主线程吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在iPhone应用程序中使用 NSURLConnection ,并且在发送 initWithRequest:后,界面似乎变慢了我的 NSURLConnection 实例。我不确定是否发生这种情况是因为我的处理代码需要很长时间来处理响应,或者是因为 NSURLConnection 阻塞了主线程。

I'm using NSURLConnection in an iPhone application and the interface seems to slow down after sending initWithRequest: to my NSURLConnection instance. I'm not sure if this is occurring because my processing code is taking a long time to handle the response, or if it's because NSURLConnection is blocking the main thread.

任何人都可以确认 NSURLConnection 将创建连接并等待单独线程上的数据,然后调用其委托方法主线程?

Can anyone confirm that NSURLConnection will create the connection and wait for data on a separate thread, and then call its delegate methods on the main thread?

谢谢!

推荐答案

NSURLConnection支持两种模式操作:异步和同步。根本不使用单独的线程。它们都只使用一个线程,即你运行它们的任何线程。

NSURLConnection supports two modes of operation: asynchronous and synchronous. Neither uses separate threads at all. They both use just one thread, that being whatever thread you run them in.

在同步模式下,NSURLConnection将阻止你运行它的任何线程。异步模式使用运行循环行为(从开发人员的角度来看)类似于后台线程,但开销较低且没有任何线程安全问题。如果使用异步模式,则需要在主线程中运行它。它不会阻止任何内容。

In synchronous mode, NSURLConnection will block whatever thread you run it in. Asynchronous mode uses the run loop to behave (from the developer's perspective) similarly to a background thread but with lower overhead and without any thread-safety issues. If using asynchronous mode, you want to run it in the main thread. It won't block anything.

如果您的接口速度变慢,这与同步使用NSURLConnection不一致,这会导致您的接口完全停止,直到请求为止已完成。

If your interface is slowing down, that is not consistent with using NSURLConnection synchronously, which would instead cause your interface to stop completely until the request is complete.

这篇关于NSURLConnection阻止主线程吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆