iOS runloop 机制有什么指南吗?
我正在iPhone上学习socket通信,它的指南中提到了一些关于CFRunloop
的内容(它是CFNetwork
的指南,可以在iOS上使用吗?) 哪里可以了解iOS上的runloop?API参考还不够。
I'm learning socket communication on iPhone, and its guide said something about CFRunloop
(it is a guide for CFNetwork
, can this be used on iOS?)
Where can I learn about runloop on iOS?API reference is not enough.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
请参阅 Apple 的 “运行循环”章节 em>线程编程指南。简而言之:
一个主要的陷阱是在等待来自运行循环源的回调时忘记运行运行循环。当您决定忙等待主线程上发生某些事情时,这有时会成为一个问题,但当您创建自己的线程并向该运行循环注册运行循环源时,您最有可能遇到它。您负责建立自动释放池并在非主线程上需要时运行运行循环,因为应用程序主函数不会为您执行此操作。
您最好阅读 Apple 的并发编程指南 相反,它提出了运行循环机制的替代方案,例如操作队列和调度源。 “从线程迁移”一章的“替换运行循环代码”部分建议使用调度源而不是运行循环源来处理事件。
Look at the "Run Loops" chapter of Apple's Threading Programming Guide. In brief:
One major pitfall is forgetting to run the run loop while waiting for a callback from a runloop source. This is sometimes a problem when you decide to busy-wait for something to happen on the main thread, but you're most likely to run into it when you create your own thread and register a runloop source with that runloop. You are responsible for establishing an autorelease pool and running the runloop if needed on non-main threads, since the application main function will not be there to do it for you.
You would do better to read Apple's Concurrency Programming Guide instead, which suggests alternatives to the runloop mechanism such as operation queues and dispatch sources. The "Replacing Run-Loop Code" section of the "Migrating Away from Threads" chapter suggests using dispatch sources instead of runloop sources to handle events.
查看 Apple 文档中的这些文章:
主事件循环
运行循环
有关如何通过运行循环运行异步连接的示例代码:
SimpleURLConnections< /a> (虽然这个使用了 NSURLConnection API)
Have a look at these articles in the Apple docs:
Main event loop
Run Loops
For sample code on how to run asynchronous connections via the run loop:
SimpleURLConnections (although this one uses the NSURLConnection API)
NSRunLoop 是一个
事件驱动模式
(androidHandler-Looper-MessageQueue
和memcached
等)这个项目(由我赞助)可以为您提供更多信息。
相信这个项目可以帮助你了解iOS/Mas Runloop清晰、充分,以便您可以正确、有效地使用 runloop 来设计您的项目。
希望这可以帮助你。
NSRunLoop is an
Event-Driven-Mode
(androidHandler-Looper-MessageQueue
andmemcached
etc)This project(sponsor by me) can give you more information.
I believe this project can help you understand iOS/Mas Runloop clearly and sufficiently, so you can design your project by using runloop correctly and effectively.
Hope this can help you.