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會提供最近的信息,并且報告有多少事件傳遞過來自從上次事件調用以來。