iPhone 开发 - 主线程

发布于 2024-11-27 19:51:08 字数 234 浏览 0 评论 0原文

我是 iPhone 开发新手,我想问一个有关异步事件的问题。

假设我有一个 NSURLConnection 和相应的委托方法,即。 didReceiveResponsedidFailWithError 等。当事件触发时,这些方法会被异步调用。所有方法都在主线程执行吗?或者iOS是否创建单独的线程来执行相应的方法代码?

我的应用程序面临一些随机崩溃,我猜这是同步问题。

I am new to iPhone development and I would like to ask a question concerning asynchronous events.

Supposing I have a NSURLConnection and the correspoding delegate methods ie. didReceiveResponse, didFailWithError etc. The methods are called asynchronously when events are fired. Are all methods executed on the main thread? Or does the iOS create separate threads that execute the corresponding method code?

I am facing some random crashes to my app, and I guess that it is a synchronization issue.

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

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

发布评论

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

评论(1

久夏青 2024-12-04 19:51:08

NSURLConnection 的委托方法在主线程中执行。这就是异步背后的全部原因,不需要有单独的线程。

关于内部结构,I/O 本质上是一个异步世界,因此我也不认为 N​​SURLConnection 内部使用线程。我认为操作系统以异步方式管理通信,但我不确定。

无论如何,您可以确保您的委托方法是从主线程内执行的。

The delegate methods of NSURLConnection are executed in the main thread. That is the whole reason behind being asynchronous, no need to have a separate thread.

About the internals, I/O is an inherently asynchronous world, so I also do not think that internally NSURLConnection uses threading. I suppose that it is the OS that manages the communication in a async way, but I am not sure about it.

Anyway, you can be sure that your delegate methods are executed from within the main thread.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文