RunLoop 从表面意思来看运行循环的意思,其实质上也真的很像循环。其实在OC中 RunLoop是用来管理线程的。每一个线程都有一个RunLoop对象。可以通过具体的方法去获得。
尽管RunLoop在开发中我们一直在用,但是没有注意他。要想理解RunLoop,首先我们需要先了解一下程序运行机制。
程序运行机制:我们都知道OC是运行时语言,也就是说对象的类型是在程序运行的时候确定的,调用类与对象相应的方法。但是最终代码的执行始终是面向过程的。
线程也是一样:一个线程从开始代码执行,到结束代码销毁。app如何实现这样的机制:app从运行开始一直处于待命状态,接收到事件之后执行操作,操作完成后继续等待相应,直到程序终止运行。这样的管理线程执行任务的机制就是RunLoop机制。线程在执行中的休眠和激活就是由RunLoop对象进行管理的。
虽然每一个线程都可以获取RUnLoop对象,但是并不是每一个线程中都有实例对象,我们可以这样理解:如果我们不获取RunLoop,这个RunLoop就不存在,我们获取时,如果不存在,就会去创建。在主线程中,这个MainRunLoop是默认创建并运行激活的。
一般来说获取runloop的方法有
1、获取主线程的NSRunLoop
//获取主线程的NSRunLoop
+(NSRunLoop *)mainRunLoop;
2、获取当前线程的NSRunLoop
//获取当前线程的RunLoop:有的话就直接获取,没有的话就自动创建
+(NSRunLoop *)currentRunLoop;
NSRunLoop* loop1=[NSRunLoop mainRunLoop];
NSLog(@"1---%@",loop1);
NSRunLoop* loop2=[NSRunLoop currentRunLoop];
NSLog(@"2---%@",loop2);
//打印结果如下 内容比较多 只截取部分
1---{wakeup port = 0x1e03, stopped = false, ignoreWakeUps = false, current mode = UIInitializationRunLoopMode,common modes ={type = mutable set, count = 2,entries =>0 :{contents = "UITrackingRunLoopMode"}2 :{contents = "kCFRunLoopDefaultMode"}},common mode items ={type = mutable set, count = 15,
entries =>........
获取RunLoop的CFRunLoopRef对象
-(CFRunLoopREF)getCFRunLoop;
NSRunLoop* loop1=[NSRunLoop mainRunLoop];
NSLog(@"1---%@",loop1);
[loop1 getCFRunLoop];
NSLog(@"ref---%@",[loop1 getCFRunLoop]);
结果如下
ref---{wakeup port = 0x1e03, stopped = false, ignoreWakeUps = false, current mode = UIInitializationRunLoopMode,common modes ={type = mutable set, count = 2,entries =>0 :{contents = "UITrackingRunLoopMode"}2 :{contents =
将定时器添加到RunLoop中
-(void)loopTest
{NSRunLoop* loop1=[NSRunLoop mainRunLoop];
NSLog(@"1---%@",loop1);
[loop1 getCFRunLoop];
NSLog(@"ref---%@",[loop1 getCFRunLoop]);
// NSRunLoop* loop2=[NSRunLoop currentRunLoop];
// NSLog(@"2---%@",loop2);
NSTimer* timer=[NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(givelog:) userInfo:nil repeats:YES];
[loop1 addTimer:timer forMode:NSDefaultRunLoopMode];
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(10 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
//移除定时器 不然会一直执行
[timer invalidate];
NSLog(@"1---%@",@"移除成功");
});
}
-(void)givelog:(NSTimer*)time
{
NSLog(@"haha");
}
2017-08-30 15:20:39.533 runtimeTest[6010:148172] haha
2017-08-30 15:20:40.533 runtimeTest[6010:148172] haha
2017-08-30 15:20:41.534 runtimeTest[6010:148172] haha
2017-08-30 15:20:42.534 runtimeTest[6010:148172] haha
2017-08-30 15:20:43.534 runtimeTest[6010:148172] haha
2017-08-30 15:20:44.534 runtimeTest[6010:148172] haha
2017-08-30 15:20:45.534 runtimeTest[6010:148172] haha
2017-08-30 15:20:46.534 runtimeTest[6010:148172] haha
2017-08-30 15:20:47.534 runtimeTest[6010:148172] haha
2017-08-30 15:20:48.533 runtimeTest[6010:148172] haha
2017-08-30 15:20:48.534 runtimeTest[6010:148172] 1---移除成功
某些延迟函数和选择器在分线程中的使用,我们必须手动开始RunLoop
@interfaceNSObject (NSDelayedPerforming)
- (void)performSelector:(SEL)aSelector withObject:(id)anArgument afterDelay:(NSTimeInterval)delay inModes:(NSArray *)modes;
- (void)performSelector:(SEL)aSelector withObject:(id)anArgument afterDelay:(NSTimeInterval)delay;
+ (void)cancelPreviousPerformRequestsWithTarget:(id)aTarget selector:(SEL)aSelectorobject:(id)anArgument;
+ (void)cancelPreviousPerformRequestsWithTarget:(id)aTarget;
- (void)performSelector:(SEL)aSelector target:(id)target argument:(id)arg order:(NSUInteger)order modes:(NSArray *)modes;
- (void)cancelPerformSelector:(SEL)aSelector target:(id)target argument:(id)arg;
- (void)cancelPerformSelectorsWithTarget:(id)target;