码迷,mamicode.com
首页 > 其他好文 > 详细

RunLoop 事件处理过程

时间:2015-10-27 15:32:51      阅读:198      评论:0      收藏:0      [点我收藏+]

标签:


The Run Loop Sequence of Events

Each time you run it, your thread’s run loop processes pending events and generates notifications for any attached observers. The order in which it does this is very specific and is as follows:

1, Notify observers that the run loop has been entered.

2, Notify observers that any ready timers are about to fire.

3, Notify observers that any input sources that are not port based are about to fire.

4, Fire any non-port-based input sources that are ready to fire.

5, If a port-based input source is ready and waiting to fire, process the event immediately. Go to step 9.

6, Notify observers that the thread is about to sleep. 

7, Put the thread to sleep until one of the following events occurs:

  • An event arrives for a port-based input source.

  • A timer fires.

  • The timeout value set for the run loop expires. 

  • The run loop is explicitly woken up. 

8, Notify observers that the thread just woke up. 

9,Process the pending event.

  • If a user-defined timer fired, process the timer event and restart the loop. Go to step 2.

  • ??If an input source fired, deliver the event.??

  • ??If the run loop was explicitly woken up but has not yet timed out, restart the loop.?? Go to step 2. 

10, Notify observers that the run loop has exited. 


从 步骤2 到 步骤9 称为 一个 loop

从整个过程可以看出,

  •     Port-based input source 优先级是最高的,得到优先处理,但是port event 并不屏蔽其他事件,处理完 port event 可以在同一个loop中继续处理其它事件(Timer 、 custom、 selector)

  •     Timer事件比较特殊, 从第九步可以看出每次 timer 事件触发后,会导致loop从新开始,其余事件(custom、 selector)只能在新的loop得到处理机会。

  •     runMode:beforeDate方法执行一次,内部可能发生 1次 或 多次loop

  •     除了Port-based event 和 Timer event, RunLoop 的 runMode:beforeDate 方法每次处理完一个 input source 事件(包括custom 、selector)或者time out就会返回。



RunLoop 事件处理过程

标签:

原文地址:http://my.oschina.net/u/255456/blog/522672

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!