iPhone 开发 - 主线程
我是 iPhone 开发新手,我想问一个有关异步事件的问题。
假设我有一个 NSURLConnection 和相应的委托方法,即。 didReceiveResponse
、didFailWithError
等。当事件触发时,这些方法会被异步调用。所有方法都在主线程执行吗?或者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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
NSURLConnection 的委托方法在主线程中执行。这就是异步背后的全部原因,不需要有单独的线程。
关于内部结构,I/O 本质上是一个异步世界,因此我也不认为 NSURLConnection 内部使用线程。我认为操作系统以异步方式管理通信,但我不确定。
无论如何,您可以确保您的委托方法是从主线程内执行的。
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.