iOS runloop 机制有什么指南吗?

发布于 2024-11-11 14:07:24 字数 136 浏览 0 评论 0原文

我正在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 技术交流群。

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

发布评论

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

评论(3

许一世地老天荒 2024-11-18 14:07:24

请参阅 Apple 的 “运行循环”章节 em>线程编程指南。简而言之:

  • 每个线程都有一个关联的运行循环。
  • 必须运行运行循环才能执行任何操作。 Apple 的应用程序 main 函数在主线程上为您处理这个问题。
  • 运行循环以特定模式运行。 “通用模式”实际上是一组模式,并且有一个 API 用于向该组添加模式。
  • 运行循环的主要目的是监视计时器和运行循环源。每个源都注册到特定模式的特定运行循环中,并且仅当运行循环在该模式下运行时才会在适当的时间进行检查。
  • 运行循环在每个循环中都会经历几个阶段,例如检查计时器和检查其他事件源。如果它发现任何源已准备好触发,则会触发适当的回调。
  • 除了使用现成的运行循环工具之外,您还可以创建自己的运行循环源以及注册运行循环观察者来跟踪运行循环的进度。

一个主要的陷阱是在等待来自运行循环源的回调时忘记运行运行循环。当您决定忙等待主线程上发生某些事情时,这有时会成为一个问题,但当您创建自己的线程并向该运行循环注册运行循环源时,您最有可能遇到它。您负责建立自动释放池并在非主线程上需要时运行运行循环,因为应用程序主函数不会为您执行此操作。

您最好阅读 Apple 的并发编程指南 相反,它提出了运行循环机制的替代方案,例如操作队列和调度源。 “从线程迁移”一章的“替换运行循环代码”部分建议使用调度源而不是运行循环源来处理事件。

Look at the "Run Loops" chapter of Apple's Threading Programming Guide. In brief:

  • There is one run loop associated with each thread.
  • The run loop has to be run to do anything. Apple's application main function takes care of this for you on the main thread.
  • A run loop is run in a specific mode. The "common mode" is actually a set of modes, and there is an API for adding modes to that set.
  • A run loop's main purpose is to monitor timers and run loop sources. Each source is registered with a specific run loop for a specific mode, and will only be checked at the appropriate time when the runloop is running in that mode.
  • The run loop goes through several stages in each go around its loop, such as checking timers and checking other event sources. If it finds that any source is ready to fire, it triggers the appropriate callback.
  • Aside from using ready-made run loop tools, you can create your own run loop sources as well as registering a run loop observer to track the progress of the run loop.

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.

残疾 2024-11-18 14:07:24

查看 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)

相对绾红妆 2024-11-18 14:07:24

NSRunLoop 是一个事件驱动模式(android Handler-Looper-MessageQueuememcached 等)

我们使用pipe()生成两个fd(一个用于read,另一个用于write)。我们可以通过向其他线程中的write fd 写入一些字节来唤醒线程(读取read fd)。

这个项目(由我赞助)可以为您提供更多信息。

https://github.com/wuyunfeng/LightWeightRunLoop

相信这个项目可以帮助你了解iOS/Mas Runloop清晰、充分,以便您可以正确、有效地使用 runloop 来设计您的项目。

希望这可以帮助你。

NSRunLoop is an Event-Driven-Mode(android Handler-Looper-MessageQueue and memcached etc)

We use pipe() to generate two fd(one for read, another for write). We can wakeup the thread (who read the read fd) by writing some bytes to write fd in other thread.

This project(sponsor by me) can give you more information.

https://github.com/wuyunfeng/LightWeightRunLoop

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.

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