转自:http://blog.youkuaiyun.com/lingedeng/article/details/6870692
如果你在非main thread中运行run loop,你必须至少为该run loop添加一个input sources或timer。如果你运行的run loop没有监控任何的输入源,该run loop将在你运行后立即退出。
Run loop observer
使用detachNewThreadSelector:toTarget:withObject:创建一个thread:
- [NSThreaddetachNewThreadSelector:@selector(observerRunLoop)toTarget:selfwithObject:nil];
在新thread的run loop中添加observer:
- -(void)observerRunLoop{
- //建立自动释放池
- NSAutoreleasePool*pool=[[NSAutoreleasePoolalloc]init];
- //获得当前thread的Runloop
- NSRunLoop*myRunLoop=[NSRunLoopcurrentRunLoop];
- //设置Runloopobserver的运行环境
- CFRunLoopObserverContextcontext={0,self,NULL,NULL,NULL};
- //创建Runloopobserver对象
- //第一个参数用于分配observer对象的内存
- //第二个参数用以设置observer所要关注的事件,详见回调函数myRunLoopObserver中注释
- //第三个参数用于标识该observer是在第一次进入runloop时执行还是每次进入runloop处理时均执行
- //第四个参数用于设置该observer的优先级
- //第五个参数用于设置该observer的回调函数
- //第六个参数用于设置该observer的运行环境
- CFRunLoopObserverRefobserver=CFRunLoopObserverCreate(kCFAllocatorDefault,kCFRunLoopAllActivities,YES,0,&myRunLoopObserver,&context);
- if(observer){
- //将Cocoa的NSRunLoop类型转换成CoreFoundation的CFRunLoopRef类型
- CFRunLoopRefcfRunLoop=[myRunLoopgetCFRunLoop];
- //将新建的observer加入到当前thread的runloop
- CFRunLoopAddObserver(cfRunLoop,observer,kCFRunLoopDefaultMode);
- }
- //CreatesandreturnsanewNSTimerobjectandschedulesitonthecurrentrunloopinthedefaultmode
- [NSTimerscheduledTimerWithTimeInterval:0.1target:selfselector:@selector(doFireTimer:)userInfo:nilrepeats:YES];
- NSIntegerloopCount=10;
- do{
- //启动当前thread的loop直到所指定的时间到达,在loop运行时,runloop会处理所有来自与该runloop联系的inputsource的数据
- //对于本例与当前runloop联系的inputsource只有一个Timer类型的source。
- //该Timer每隔0.1秒发送触发事件给runloop,runloop检测到该事件时会调用相应的处理方法。
- //由于在runloop添加了observer且设置observer对所有的runloop行为都感兴趣。
- //当调用runUnitDate方法时,observer检测到runloop启动并进入循环,observer会调用其回调函数,第二个参数所传递的行为是kCFRunLoopEntry。
- //observer检测到runloop的其它行为并调用回调函数的操作与上面的描述相类似。
- [myRunLooprunUntilDate:[NSDatedateWithTimeIntervalSinceNow:1.0]];
- //当runloop的运行时间到达时,会退出当前的runloop。observer同样会检测到runloop的退出行为并调用其回调函数,第二个参数所传递的行为是kCFRunLoopExit。
- loopCount--;
- }while(loopCount);
- //释放自动释放池
- [poolrelease];
- }
设置observer的回调函数:
- voidmyRunLoopObserver(CFRunLoopObserverRefobserver,CFRunLoopActivityactivity,void*info){
- switch(activity){
- //Theentranceoftherunloop,beforeenteringtheeventprocessingloop.
- //ThisactivityoccursonceforeachcalltoCFRunLoopRunandCFRunLoopRunInMode
- casekCFRunLoopEntry:
- NSLog(@"runloopentry");
- break;
- //Insidetheeventprocessingloopbeforeanytimersareprocessed
- casekCFRunLoopBeforeTimers:
- NSLog(@"runloopbeforetimers");
- break;
- //Insidetheeventprocessingloopbeforeanysourcesareprocessed
- casekCFRunLoopBeforeSources:
- NSLog(@"runloopbeforesources");
- break;
- //Insidetheeventprocessingloopbeforetherunloopsleeps,waitingforasourceortimertofire.
- //ThisactivitydoesnotoccurifCFRunLoopRunInModeiscalledwithatimeoutof0seconds.
- //Italsodoesnotoccurinaparticulariterationoftheeventprocessingloopifaversion0sourcefires
- casekCFRunLoopBeforeWaiting:
- NSLog(@"runloopbeforewaiting");
- break;
- //Insidetheeventprocessingloopaftertherunloopwakesup,butbeforeprocessingtheeventthatwokeitup.
- //Thisactivityoccursonlyiftherunloopdidinfactgotosleepduringthecurrentloop
- casekCFRunLoopAfterWaiting:
- NSLog(@"runloopafterwaiting");
- break;
- //Theexitoftherunloop,afterexitingtheeventprocessingloop.
- //ThisactivityoccursonceforeachcalltoCFRunLoopRunandCFRunLoopRunInMode
- casekCFRunLoopExit:
- NSLog(@"runloopexit");
- break;
- /*
- Acombinationofalltheprecedingstages
- casekCFRunLoopAllActivities:
- break;
- */
- default:
- break;
- }
- }
启动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概念”。