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.
每當你與系統(tǒng)進行交互時王污,你必須會話費大量的時間為那個任務準備好蛉鹿。調(diào)用內(nèi)核或者涉及到上下文變化的系統(tǒng)其它其他的系統(tǒng)層和自己進程的調(diào)用相比灸姊,需要耗費大量的時間。因此布蔗,許多系統(tǒng)庫提供異步接口赢底,當你發(fā)送這個請求后,這個請求正在處理的過程中缚态,你可以繼續(xù)干其他事情扮碧。GCD就是這樣干的趟章,讓你可以可以在提交請求后,在回調(diào)中有結果芬萍。
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是一種基礎數(shù)據(jù)類型尤揣,用來匹配低水平系統(tǒng)層級的事件。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
數(shù)據(jù)可讀的時候
When it is possible to write data
數(shù)據(jù)可寫的時候
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
當進程發(fā)出fork或exec的時候
When a signal is delivered to the process
當信號被發(fā)送到進程的時候
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莽龟,你自己調(diào)用蠕嫁。
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取代了用于處理系統(tǒng)相關事件的回調(diào)函數(shù)。當你配置Dispatch sources的時候毯盈,你要指定你想要監(jiān)聽的事件和調(diào)度隊列剃毒,以及處理這些事件的代碼。你也可以使用block或者函數(shù)搂赋。當事件到達的時候赘阀,Dispatch sources會把block添加到隊列中進行執(zhí)行。
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會為你的程序提供連續(xù)的事件源基公。dispatch sources會保留在他的dispatch queue中直到顯示取消。當保留的時候宋欺,它會提交相關的任務代碼到隊列中轰豆,無論相關事件什么時候發(fā)生。一些事件齿诞,像定時器事件酸休,有規(guī)律發(fā)生但是大多數(shù)只在特定條件下發(fā)生。因此祷杈,dispatch sources會保留相關的度調(diào)度隊列斑司,以防止過早的釋放,當時間處于待處理的狀態(tài)下吠式。
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實現(xiàn)一個合并的計劃特占。如果一個新事件在其他事件出列和執(zhí)行之前到達糙置。dispatch source會把舊事件和新事件進行合并。根據(jù)情況是目,合并可能取代事件或更新信息谤饭。例如:signal-based dispatch source會提供最近的信息,并且報告有多少事件傳遞過來自從上次事件調(diào)用以來懊纳。