iOS runloop机制有什么指南吗? [英] Is there any guide for iOS runloop mechanism?

查看:126
本文介绍了iOS runloop机制有什么指南吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在学习iPhone上的套接字通信,它的指南说了一些关于 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.

推荐答案

查看Run Loops一章线程编程指南>。简而言之:

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


  • 每个线程都有一个运行循环。

  • 必须运行run循环才能执行任何操作。 Apple的应用程序主要功能在主线程上为您处理。

  • 运行循环以特定模式运行。 共模实际上是一组模式,并且有一个API用于向该组添加模式。

  • 运行循环的主要目的是监视计时器并运行循环源。每个源都使用特定模式的特定运行循环进行注册,并且仅在runloop以该模式运行时的适当时间进行检查。

  • 运行循环经历几个阶段在每个循环中,例如检查计时器和检查其他事件源。如果它发现任何源已准备好触发,它会触发相应的回调。

  • 除了使用现成的运行循环工具,您还可以创建自己的运行循环源以及注册一个运行循环观察器来跟踪运行循环的进度。

  • 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.

一个主要的缺陷是忘记在等待运行循环时运行循环来自runloop源的回调。当您决定忙于等待主线程上发生的事情时,这有时会出现问题,但是当您创建自己的线程并使用该runloop注册runloop源时,您最有可能遇到此问题。你负责建立一个自动释放池并在非主线程上运行runloop,因为应用程序的主要功能不适合你。

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.

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

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.

这篇关于iOS runloop机制有什么指南吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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