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

消化消化,吃饭去。有时间继续

时间: 2024-10-04 20:31:03

NSRunLoop(来自官方文档)的相关文章

学习使用Volley的多种基本功能-来自官方文档

官方文档地址:https://developer.android.com/training/volley/requestqueue.html 之前一直没看到这个,原来官方已经正式有关于Volley的文档了,而且这是被建议使用的Network库. 因为之前有写过简单的Volley请求怎么写,下面介绍的都是我之前不知道的用法: 1.先上代码: 1 Cache cache = new DiskBasedCache(getCacheDir(),1024*1024); 2 Network network

MySQL学习 --来自官方文档的翻译

通用知识: 1.mysql>source path; path:要执行的sql路径 学会使用help content;里面会有详细的说明和例子 第一篇 字符集 1.基础知识 级别:服务器.数据库.表.属性 character set和collations 的区别 字符集:一组符号和其编码的集合 排序规则:符合编码的比较规则,最简单的比较规则是二进制排序规则. MySQL可以做的事情: 使用大量的字符集和排序规则组合来表示字符串,其应用水平可以在数据库.表.属性等级别. 然而要想知道如何高效利用M

Delivering touch events to a view outside the bounds of its parent view(来自官方文档)

Q: My view is displayed correctly on the screen, but does not receive any touch events. Why is that? A: The most common cause of this problem is because your view is located outside the bounds of its parent view. When your application receives a touc

Android官方文档之Introduction

写在前面的话:接触Android的时间也不短了,听了视频.看了书.敲了代码,写了博客,做了demo...但是想做出一款优秀的APP(哪怕是封装一个不错的功能)还有很长的路要走.于是前些日子我打算更加深入地往底层.往源代码方向研究Android--我就买了一本<Android群英传>拜读一下,在刚读到前言的时候,我发现作者推荐了阅读官方的Training和Guide,我才意识到,其实之前我接触到的各个渠道的Android知识,都是来自官方文档,与其更加深入地了解Android,不如把官方文档的内

官方文档 恢复备份指南三 Recovery Manager Architecture

本节讨论以下问题: About the RMAN Environment                        关于RMAN环境 RMAN Command-Line Client                            RMAN命令行 RMAN Channels                                                      RMAN通道 RMAN Repository                                

常用SQL_官方文档使用

SQL语句基础理论 SQL是操作和检索关系型数据库的标准语言,标准SQL语句可用于操作关系型数据库. 5大主要类型: ①DQL(Data Query Language,数据查询语言)语句,主要由于select关键字完成,查询语句是SQL语句中最复杂,功能最丰富的语句. ②DML(Data Munipulation Language,数据操作语言)语句,DML语句修改后数据将保持较好的一致性:操作表的语句,如增插insert.更新update.删除delete等: ③DDL(Data Defini

Erlang epmd官方文档中文翻译

本文含epmd简介及官方文档之翻译,文档地址 http://erlang.org/doc/man/epmd.html翻译时的版本 R19.1 中英文水平都不咋地,不通顺处海涵,就酱. 简介 Erlang分布式系统中节点是通过节点名字互相连接的,节点名字为[email protected]_ADDRESS格式. epmd是分布式erlang中比较重要的模块.集群中每台机器都有一个epmd进程,这些进程端口号都用同一个端口号(默认4396端口).所有节点启动的时候都会连接到本机对应的epmd进程,它

OpenGL ES着色器语言之语句和结构体(官方文档第六章)内建变量(官方文档第七、八章)

OpenGL ES着色器语言之语句和结构体(官方文档第六章) OpenGL ES着色器语言的程序块基本构成如下: 语句和声明 函数定义 选择(if-else) 迭代(for, while, do-while) 跳跃(discard, return, break, continue) 6.1函数定义   着色器是由一系列全局声明和函数定义组成的.函数声明规范如下: // prototype returnType functionName (type0 arg0, type1 arg1, ...,

hbase官方文档(转)

Apache HBase™ 参考指南  HBase 官方文档中文版 Copyright © 2012 Apache Software Foundation.保留所有权利. Apache Hadoop, Hadoop, MapReduce, HDFS, Zookeeper, HBase 及 HBase项目 logo 是Apache Software Foundation的商标. Revision History Revision 0.95-SNAPSHOT 2012-12-03T13:38 中文版