NSRunLoop(来自官方文档)
The NSRunLoop class declares the programmatic interface to objects that manage input sources. An NSRunLoop object processes input for sources such as mouse and keyboard events from the window system, NSPort objects, and NSConnection objects. An NSRunLoop object also processes NSTimer events.
Your application cannot either create or explicitly manage NSRunLoop objects. Each NSThreadobject, including the application’s main thread, has an NSRunLoop object automatically created for it as needed. If you need to access the current thread’s run loop, you do so with the class method currentRunLoop.
Note that from the perspective of NSRunloop, NSTimer objects are not "input"—they are a special type, and one of the things that means is that they do not cause the run loop to return when they fire.
注意:
NSRunLoop通常不被认为是线程安全的,并且只应该在当前线程的上下文环境中调用它的方法。不要尝试在另一个不同的线程中调用NSRunLoop对象的方法,这样做可能造成未知的错误。
Timer和NSRunLoop结合工作,但他们不能提供一个真正的时间机制,准确性受限。如果你只是想在未来的某个时间点发送一个消息,可以不用timer,Timer就是NSTimer对象。
Timers are represented by NSTimer objects. They work in conjunction with NSRunLoopobjects. NSRunLoop objects control loops that wait for input, and they use timers to help determine the maximum amount of time they should wait. When the timer’s time limit has elapsed, the run loop fires the timer (causing its message to be sent), then checks for new input.
The run loop mode in which you register the timer must be running for the timer to fire. For applications built using the Application Kit or UIKit, the application object runs the main thread’s run loop for you. On secondary threads, however, you have to run the run loop yourself.
RunLoop是和线程相关的基础设施。一个runloop是一个时间处理loop,你可以安排工作、协调the receipt of incoming events。runloop的目的是有工作的时候,让线程忙,没有工作,就让线程sleep。
Run loop管理不是完全自动的。你必须设计你的线程代码,在合适的时间启动run loop,并相应到来的事件。Cocoa和Core Foundation提供runloop对象帮助你配置管理你的线程的runloop。你的应用程序没有必要显式的创建这些对象;每个线程(包括应用程序主线程)有一个关联的runloop对象。只有secondar thread需要显式运行他们的runloop。app框架(frameworks)在主线程自动启动并运行runloop,作为程序启动进程的一部分。
Anatomy of a Run Loop
A run loop is very much like its name sounds. It is a loop your thread enters and uses to run event handlers in response to incoming events. Your code provides the control statements used to implement the actual loop portion of the run loop—in other words, your code provides the while or for loop that drives the run loop. Within your loop, you use a run loop object to "run” the event-processing code that receives events and calls the installed handlers.
run loop从两种不同类型的源接收事件。Input sources deliver异步事件,通常是从另一个线程或一个不同应用的消息。Timer source deliver ,不多解释了。这两种源到达时,都使用应用程序特定处理路径。
下面的图3-1显示了run loop概念上的结构和各种源(source)。Input source deliver异步事件给合适的处理者,并造成 runUntilDate: 方法(被和线程相关的runloop对象调用)退出。Timer source deliver events to their handler routines(理解,翻译不通,有个懂英语的妹子多好),但不会造成runloop退出。

In addition to handling sources of input, run loops also generate notifications about the run loop's behavior. Registered run-loop observers can receive these notifications and use them to do additional processing on the thread. You use Core Foundation to install run-loop observers on your threads.
下面的内容提供更过关于runloop组件和模式的信息,也描述了在处理事件的不同时间,生成的通知。
Run Loop Modes
A run loop mode is a collection of input sources and timers to be monitored and a collection of run loop observers to be notified. Each time you run you run loop, you specify(显式或隐式指定都行)a particular "mode" in which to run. During that pass of the run loop, only sources associated with that mode are monitored and allowed to deliver their events.(Similarly, only observers associated with that mode are notified of the run loop's progress.) Sources associated with other modes hold on to any new events until subsequent passes through the loop in the appropriate mode.(关键的东西不敢乱翻译啊,胆小)
在你的代码里,通过名字来区别不同的模式(modes)。Cocoa和Core Foundation定义一个默认的和几个常用的模式。你可以自定义模式,通过为模式的名字指定一个字符串。模式的名字可以随便起,但内容不能。你要确保一个或者更多input sources、timers或者run-loop观察者被加到你创建的模式中。
You use modes to filter out events from unwanted sources during a particular pass through your run loop. Most of time, you will want to run you run loop in the system-defined "default" mode. A modal panel, however, might run in the "modal" mode. While in this mode, only sources revevant to the model panel would deliver events to the thread. For secondary threads, you might use custom modes to prevent low-priority sources from delivering events during time-critical operations.
注意:Modes discriminate based on the source of the event, not the type of the event. For example, you would not use modes to match only mouse-down events or only keyboard events. You could use modes to listen to a different set of ports, suspend timers temporarily, or otherwise change the sources and run loop observers currently being monitored.
消化消化,吃饭去。有时间继续
NSRunLoop(来自官方文档)的更多相关文章
- citus 多租户应用开发(来自官方文档)
citus 官方文档很不错,资料很全,同时包含一个多租户应用的文档,所以运行下,方便学习 环境准备 使用docker-compose 运行,同时集成了graphql 引擎,很方便 docker-c ...
- citus real-time 分析demo( 来自官方文档)
citus 对于多租户以及实时应用的开发都是比较好的,官方也提供了demo 参考项目 https://github.com/rongfengliang/citus-hasuar-graphql 环 ...
- c++官方文档
来自官方文档...感谢老王指出需要c++11,一下代码全在c++11下编译,编译参数加入 -std=c++11 #include<stdio.h> #include<iostrea ...
- Spark官方文档 - 中文翻译
Spark官方文档 - 中文翻译 Spark版本:1.6.0 转载请注明出处:http://www.cnblogs.com/BYRans/ 1 概述(Overview) 2 引入Spark(Linki ...
- Spring 4 官方文档学习(十四)WebSocket支持
个人提示:如果需要用到页面推送,高频且要低延迟,WebSocket无疑是最佳选择.否则还是轮询和long polling吧. 做了一个小demo放在码云上,有兴趣的可以看一下,简单易懂:websock ...
- Spring 4 官方文档学习(十二)View技术
关键词:view technology.template.template engine.markup.内容较多,按需查用即可. 介绍 Thymeleaf Groovy Markup Template ...
- Spring 4 官方文档学习(十一)Web MVC 框架之异常处理
1.HandlerExceptionResolver Spring HandlerExceptionResolver的实现们会处理controller执行过程中发送的unexpected except ...
- Spark Streaming官方文档学习--上
官方文档地址:http://spark.apache.org/docs/latest/streaming-programming-guide.html Spark Streaming是spark ap ...
- Google Android官方文档进程与线程(Processes and Threads)翻译
android的多线程在开发中已经有使用过了,想再系统地学习一下,找到了android的官方文档,介绍进程与线程的介绍,试着翻译一下. 原文地址:http://developer.android.co ...
随机推荐
- UITabBarController自定义二之xib
UITabBarController自定义二之xib 新建一个xib文件 在UITabBarController的子类方法viewDidLoad方法中加载xib 1.-(void)viewDidLoa ...
- 【USACO 1.2.1】挤牛奶
[问题描述] 三个农民每天清晨5点起床,然后去牛棚给3头牛挤奶.第一个农民在300时刻(从5点开始计时,秒为单位)给他的牛挤奶,一直到1000时刻.第二个农民在700时刻开始,在 1200时刻结束.第 ...
- 88 Merge Sorted Array(归并排序Easy)
题目意思:num1和num2均为递增数组,对其进行递增排序存到num1中 class Solution { public: void merge(vector<int>& nums ...
- 如何让IIS 8.0支持无后缀图片的访问
进入“MIME类型”模块后,我们点击右侧的“添加”,然后填好文件扩展名和类型值.对于无后缀的图片文件,扩展名只需填写“点”符号即可,类型值根据图片文件实际的扩展名填写.如果是jpeg格式的,那么就填写 ...
- FLEX 网格布局及响应式处理
上一篇文章用Flex实现BorderLayout,这一章我们来实现常用的网格布局和响应式处理. 首先我们定义HTML结构,主Box为grid,每项为grid-cell,下面就是我们HTML代码结构. ...
- Websocket 与代理服务器如何交互? How HTML5 Web Sockets Interact With Proxy Servers
How HTML5 Web Sockets Interact With Proxy Servers Posted by Peter Lubberson Mar 16, 2010 With the re ...
- 网易DBA私享会分享会笔记1
1.mysql生态+DBA职业发展世界范围流行的开源关系型的数据库关系型数据库:mysql,oracle,sql server,access,postgresql,sqlite,sybase,info ...
- UVa 673 Parentheses Balance(栈的使用)
栈 Time Limit:3000MS Memory Limit:0KB 64bit IO Format:%lld & %llu Description You are ...
- 转:CentOS6.3配置yum源
全新以最小化包安装了64位的CentOS6.3系统,作为本地的Web服务器使用,现记录全过程第二步,配置网易163的yum源 1. 下载repo文件 下载地址:http://mirrors.16 ...
- JavaEmail
一.邮件相关基本概念 a)邮件服务器:某厂商提供的电子邮件服务器的集群(或群集) b)电子邮箱:邮件服务器上提供的个人或企业的邮件空间 c)邮件传输协议:邮件服务器之间,和个人或企业与邮件 ...