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

NSRunLoop 的那些事--持续更新

时间:2015-11-07 00:50:29      阅读:175      评论:0      收藏:0      [点我收藏+]

标签:

  本文主要介绍NSRunLoop概念、特性及其应用。

  一、NSRunLoop是什么、有什么用?

    我们先看官方定义:

    “The NSRunLoop class declares the programmatic interface to objects that manage input sources. An NSRunLoop object processes input for sources such as mouse and keyboard events from the window system, NSPort objects, and NSConnection objects. An NSRunLoop object also processes NSTimer events.”

    NSRunLoop类型是用来管理输入源的。一个NSRunLoop对象会处理自来鼠标,键盘,NSPort对象和NSConnetion对象输入(传入)的事件,当然也处理NSTimer的事件。

    一句话,NSRunLoop是用来处理事件的类。

    每个线程都有一个自己的NSRunLoop对象,所以,我们不用去创建NSRunLoop对象,如果需要使用,使用  currentRunLoop方法  获取NSRunLoop对象即可。

  二、NSRunLoop什么时候用

    一般来说,以下四种情况会用到NSRunLoop,

(一)Use ports or custom input sources to communicate with other threads. 

      使用端口或自定义的输入源与其他线程交互。

(二)Use timers on the thread. 

  在线程中使用定时器。

(三)Use any of the performSelector… methods in a Cocoa application. 

  使用任何performSelector开头的方法。

(四)Keep the thread around to perform periodic tasks.

  在线程中执行周期性任务。

  

  三、NSRunLoop怎么用

    其实,我们经常在使用定时器的时候用到NSRunloop。

 

NSRunLoop 的那些事--持续更新

标签:

原文地址:http://www.cnblogs.com/panyuluoye/p/4934007.html

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