iphone -线程 使用run loop对象 2


转自:http://blog.csdn.net/lingedeng/article/details/6870692

转自:http://blog.csdn.net/workhardupc100/article/details/6929680


如果你在非main thread中运行run loop,你必须至少为该run loop添加一个input sources或timer。如果你运行的run loop没有监控任何的输入源,该run loop将在你运行后立即退出。

Run loop observer

        使用detachNewThreadSelector:toTarget:withObject:创建一个thread:

  1. [NSThread detachNewThreadSelector:@selector(observerRunLoop) toTarget:self withObject:nil];  

         在新thread的run loop中添加observer:

  1. - (void)observerRunLoop {  
  2.     //建立自动释放池  
  3.     NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];  
  4.       
  5.     //获得当前thread的Run loop  
  6.     NSRunLoop *myRunLoop = [NSRunLoop currentRunLoop];  
  7.       
  8.     //设置Run loop observer的运行环境  
  9.     CFRunLoopObserverContext context = {0, self, NULL, NULL, NULL};  
  10.       
  11.     //创建Run loop observer对象  
  12.     //第一个参数用于分配observer对象的内存  
  13.     //第二个参数用以设置observer所要关注的事件,详见回调函数myRunLoopObserver中注释  
  14.     //第三个参数用于标识该observer是在第一次进入run loop时执行还是每次进入run loop处理时均执行  
  15.     //第四个参数用于设置该observer的优先级  
  16.     //第五个参数用于设置该observer的回调函数  
  17.     //第六个参数用于设置该observer的运行环境  
  18.     CFRunLoopObserverRef observer = CFRunLoopObserverCreate(kCFAllocatorDefault, kCFRunLoopAllActivities, YES, 0, &myRunLoopObserver, &context);  
  19.       
  20.       
  21.     if (observer) {  
  22.         //将Cocoa的NSRunLoop类型转换成Core Foundation的CFRunLoopRef类型  
  23.         CFRunLoopRef cfRunLoop = [myRunLoop getCFRunLoop];  
  24.         //将新建的observer加入到当前thread的run loop  
  25.         CFRunLoopAddObserver(cfRunLoop, observer, kCFRunLoopDefaultMode);  
  26.     }  
  27.       
  28.     //Creates and returns a new NSTimer object and schedules it on the current run loop in the default mode  
  29.     [NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(doFireTimer:) userInfo:nil repeats:YES];  
  30.       
  31.     NSInteger loopCount = 10;  
  32.       
  33.     do {  
  34.         //启动当前thread的loop直到所指定的时间到达,在loop运行时,run loop会处理所有来自与该run loop联系的input source的数据  
  35.         //对于本例与当前run loop联系的input source只有一个Timer类型的source。  
  36.         //该Timer每隔0.1秒发送触发事件给run loop,run loop检测到该事件时会调用相应的处理方法。  
  37.           
  38.         //由于在run loop添加了observer且设置observer对所有的run loop行为都感兴趣。  
  39.         //当调用runUnitDate方法时,observer检测到run loop启动并进入循环,observer会调用其回调函数,第二个参数所传递的行为是kCFRunLoopEntry。  
  40.         //observer检测到run loop的其它行为并调用回调函数的操作与上面的描述相类似。  
  41.         [myRunLoop runUntilDate:[NSDate dateWithTimeIntervalSinceNow:1.0]];  
  42.         //当run loop的运行时间到达时,会退出当前的run loop。observer同样会检测到run loop的退出行为并调用其回调函数,第二个参数所传递的行为是kCFRunLoopExit。  
  43.           
  44.         loopCount--;  
  45.     } while (loopCount);  
  46.       
  47.     //释放自动释放池  
  48.     [pool release];  
  49. }  

         设置observer的回调函数:

  1. void myRunLoopObserver(CFRunLoopObserverRef observer, CFRunLoopActivity activity, void *info) {  
  2.     switch (activity) {  
  3.         //The entrance of the run loop, before entering the event processing loop.   
  4.         //This activity occurs once for each call to CFRunLoopRun and CFRunLoopRunInMode  
  5.         case kCFRunLoopEntry:  
  6.             NSLog(@"run loop entry");  
  7.             break;  
  8.         //Inside the event processing loop before any timers are processed  
  9.         case kCFRunLoopBeforeTimers:  
  10.             NSLog(@"run loop before timers");  
  11.             break;  
  12.         //Inside the event processing loop before any sources are processed  
  13.         case kCFRunLoopBeforeSources:  
  14.             NSLog(@"run loop before sources");  
  15.             break;  
  16.         //Inside the event processing loop before the run loop sleeps, waiting for a source or timer to fire.   
  17.         //This activity does not occur if CFRunLoopRunInMode is called with a timeout of 0 seconds.   
  18.         //It also does not occur in a particular iteration of the event processing loop if a version 0 source fires  
  19.         case kCFRunLoopBeforeWaiting:  
  20.             NSLog(@"run loop before waiting");  
  21.             break;  
  22.         //Inside the event processing loop after the run loop wakes up, but before processing the event that woke it up.   
  23.         //This activity occurs only if the run loop did in fact go to sleep during the current loop  
  24.         case kCFRunLoopAfterWaiting:  
  25.             NSLog(@"run loop after waiting");  
  26.             break;  
  27.         //The exit of the run loop, after exiting the event processing loop.   
  28.         //This activity occurs once for each call to CFRunLoopRun and CFRunLoopRunInMode  
  29.         case kCFRunLoopExit:  
  30.             NSLog(@"run loop exit");  
  31.             break;  
  32.         /* 
  33.          A combination of all the preceding stages 
  34.         case kCFRunLoopAllActivities: 
  35.             break; 
  36.         */  
  37.         default:  
  38.             break;  
  39.     }  
  40. }  

启动run loop

        启动run loop的方法:无条件启动,设置时间限制启动,在特殊的模式下启动。

        以无条件模式进入run loop是最简单的选择,但并不是最好的选择。以无条件的形式运行run loop将使thread进入一个永久的循环,这样的操作会使用户很难对run loop进行控制。你可以为该run loop添加input source或timer,但能退出该run loop的方法就是kill。这种启动情况下并不能让run loop运行于自定义模式中。

        不同于无条件方式运行run loop,使用时间限制的方式启动run loop更好。当你使用超时时间来对run loop的运行加以限制,则run loop一直运行直至事件到达或达到超时时间。如果是事件到达,run loop将事件分发给handler(处理器)进行处理并在处理完成后退出。你的代码然后重启run loop来处理下一个事件。如果是因到达超时时间而退出,you can simply restart the run loop or use the time to do any needed housekeeping。

       除了超时时间,你还可以运行run loop在特定的模式下。模式和超时时间并不互斥,你可以在启动一个run loop时同时指定超时时间和模式。模式限制了投递事件给run loop的sources的类型,详细的见“iphone——NSRunLoop概念”。

退出run loop

         退出run loop的方法:以超时时间配置run loop启动,显式的停止run loop(调用CFRunLoopStop函数)。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值