Whenever you interact with the underlying system, you must be prepared for that task to take a nontrivial amount of time. Calling down to the kernel or other system layers involves a change in context that is reasonably expensive compared to calls that occur within your own process. As a result, many system libraries provide asynchronous interfaces to allow your code to submit a request to the system and continue to do other work while that request is processed. Grand Central Dispatch builds on this general behavior by allowing you to submit your request and have the results reported back to your code using blocks and dispatch queues.
每当你与系统进行交互时,你必须会话费大量的时间为那个任务准备好。调用内核或者涉及到上下文变化的系统其它其他的系统层和自己进程的调用相比,需要耗费大量的时间。因此,许多系统库提供异步接口,当你发送这个请求后,这个请求正在处理的过程中,你可以继续干其他事情。GCD就是这样干的,让你可以可以在提交请求后,在回调中有结果。
About Dispatch Sources
关于Dispatch Sources
A dispatch source is a fundamental data type that coordinates the processing of specific low-level system events. Grand Central Dispatch supports the following types of dispatch sources:
Dispatch Sources是一种基础数据类型,用来匹配低水平系统层级的事件。GCD支持下边这几种Dispatch Sources。
Timer dispatch sources generate periodic notifications.
定时器会生成定期的通知。
Signal dispatch sources notify you when a UNIX signal arrives.
Signal dispatch sources会通知你,在Unix信号到达的时候。
Descriptor sources notify you of various file- and socket-based operations, such as:
Descriptor sources会在文件操作的时候通知你:
When data is available for reading
数据可读的时候
When it is possible to write data
数据可写的时候
When files are deleted, moved, or renamed in the file system
文件被删除。移动,重命名的时候
When file meta information changes
文件原信息改变的时候
Process dispatch sources notify you of process-related events, such as:
Process dispatch sources会通知你当事件变化的时候
When a process exits
进程终止的时候
When a process issues a fork or exec type of call
当进程发出fork或exec的时候
When a signal is delivered to the process
当信号被发送到进程的时候
Mach port dispatch sources notify you of Mach-related events.
Mach port dispatch sources会通知你mach相关事件
Custom dispatch sources are ones you define and trigger yourself.
自定义事件是你自定义的source,你自己调用。
Dispatch sources replace the asynchronous callback functions that are typically used to process system-related events. When you configure a dispatch source, you specify the events you want to monitor and the dispatch queue and code to use to process those events. You can specify your code using block objects or functions. When an event of interest arrives, the dispatch source submits your block or function to the specified dispatch queue for execution.
Dispatch sources取代了用于处理系统相关事件的回调函数。当你配置Dispatch sources的时候,你要指定你想要监听的事件和调度队列,以及处理这些事件的代码。你也可以使用block或者函数。当事件到达的时候,Dispatch sources会把block添加到队列中进行执行。
Unlike tasks that you submit to a queue manually, dispatch sources provide a continuous source of events for your application. A dispatch source remains attached to its dispatch queue until you cancel it explicitly. While attached, it submits its associated task code to the dispatch queue whenever the corresponding event occurs. Some events, such as timer events, occur at regular intervals but most occur only sporadically as specific conditions arise. For this reason, dispatch sources retain their associated dispatch queue to prevent it from being released prematurely while events may still be pending.
和你手动添加到队列中的任务不一样,dispatch sources会为你的程序提供连续的事件源。dispatch sources会保留在他的dispatch queue中直到显示取消。当保留的时候,它会提交相关的任务代码到队列中,无论相关事件什么时候发生。一些事件,像定时器事件,有规律发生但是大多数只在特定条件下发生。因此,dispatch sources会保留相关的度调度队列,以防止过早的释放,当时间处于待处理的状态下。
To prevent events from becoming backlogged in a dispatch queue, dispatch sources implement an event coalescing scheme. If a new event arrives before the event handler for a previous event has been dequeued and executed, the dispatch source coalesces the data from the new event data with data from the old event. Depending on the type of event, coalescing may replace the old event or update the information it holds. For example, a signal-based dispatch source provides information about only the most recent signal but also reports how many total signals have been delivered since the last z of the event handler.
在队列中,为了防止事件积压,dispatch sources实现一个合并的计划。如果一个新事件在其他事件出列和执行之前到达。dispatch source会把旧事件和新事件进行合并。根据情况,合并可能取代事件或更新信息。例如:signal-based dispatch source会提供最近的信息,并且报告有多少事件传递过来自从上次事件调用以来。