你好,歡迎來到IOS教程網

 Ios教程網 >> IOS編程開發 >> IOS開發綜合 >> iOS多線程之NSRunloop

iOS多線程之NSRunloop

編輯:IOS開發綜合

1.簡介

Runloop是與線程有關的基礎框架的一部分,是用來規劃事件處理的,當有任務的時候Runloop會讓線程處理任務,當沒有任務的時候Runloop會讓線程處於休眠狀態。
Runloop的管理不完全是自動的,我們必須在合適的時候開啟Runloop和處理到達的事件,Cocoa和Core Foundation都提供了Runloop對象來配置和管理線程的Runloop。我們的應用程序不需要顯示的創建這些對象,包括應用主線程在內的每一個線程都有一個相關的Runloop對象。而且只有第二線程是需要顯示地運行Runloop,主線程是不需要的,APP把主線程Runloop的配置和運行作為了應用程序啟動的一部分。
操作Runloop的兩個接口: 1.NSRunLoop Class Reference 2.CFRunLoop Reference

2.Runloop解析

Runloop的功能就像他的名字聽起來一樣,使線程進入一個循環,當事件到達的時候調用事件處理方法。我們要自己提供實現Runloop實際循環運行的控制語句,話句話說就是我們要提供while或者for來驅動Runloop。在循環內部我們運行Runloop,當事件到達的時候調用事件處理器。
Runloop接收來自兩種源的事件,Input sources異步的分發通常是來自於同一個應用內的另一個線程的消息事件。Timer sources同步的分發在設定好的時間發生或者循環間斷地發生的事件。這兩種事件源都是使用應用指定的事件處理方法來處理到達的事件。

下面的圖顯示了Runloop和事件源的概念結構。Input sources異步的分發事件到響應的處理器,然後引起runUntilDate:(由線程相關的Runloop對象調用)方法退出。Timer sources同步分發事件到相應的處理器但是不會引起Runloop退出。

\\

除了處理輸入源的事件,Runloop也會生成Runloop行為的通知。注冊Runloop的觀察者可以收到這些消息,然後在線程內用他們做一些額外的處理。我們只能使用CZ喎?/kf/ware/vc/" target="_blank" class="keylink">vcmUgRm91bmRhdGlvbr3Tv9rAtNeisuHP37PMtcRSdW5sb29wuduy7NXfoaM8YnI+CjwvcD4KPGgyPjIuMSBSdW5sb29w1MvQ0MSjyr08L2gyPgo8cD48L3A+CjxwPtK71tZSdW5sb29w1MvQ0MSjyr2+zcrH0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1TZWxlY3RvcioqKioqt723qKGj0OjSqsjDz9+zzNa00NDW3Mba0NS1xLmk1/eho05TVVJMQ29ubmVjdGlvbtTa19PP37PM1tC3osbw0uyyvcfrx/Ohowo8aDE+NC6yubPkPC9oMT4KPHA+PC9wPgo8aDI+NC4xIM/fs8ywssir0NQ8L2gyPgo8cD67+dPaQ29jb2G1xL3Tv9qyu8rHz9+zzLCyyKu1xKOsu/nT2kNvcmUgRm91bmRhdGlvbrXEvdO/2srHz9+zzLCyyKu1xKGjPC9wPgo8aDI+NC4yINfUtqjS5crkyOvUtLXE1K3A7c28PC9oMj4KPHA+PGltZyBzcmM9"/uploadfile/Collfiles/20140316/2014031609523955.jpg" alt=""/kf" target="_blank" class="keylink">編程Part 1/3 - NSThread & Run Loop

  1. 上一頁:
  2. 下一頁:
蘋果刷機越獄教程| IOS教程問題解答| IOS技巧綜合| IOS7技巧| IOS8教程
Copyright © Ios教程網 All Rights Reserved