1、代码实例1
-
[[NSRunLoop currentRunLoop] run];
在performSelector
之前,不执行test -
[[NSRunLoop currentRunLoop] run];
在performSelector
之后,执行test
[self performSelector:@selector(test) withObject:nil afterDelay:0];
1、此方法是,在当前runloop中添加了一个timer,用于执行aSelector message
2、会卡住当前线程(只会卡子线程哦,主线程不会卡)
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
[super touchesBegan:touches withEvent:event];
dispatch_async(dispatch_get_global_queue(0, 0), ^{
NSLog(@"1");
//RunLoop timer
// [[NSRunLoop currentRunLoop] run];//不执行test
//This method sets up a timer to perform the aSelector message on the current thread’s run loop.
//此方法是,在当前runloop中添加了一个timer,用于执行aSelector message
[self performSelector:@selector(test) withObject:nil afterDelay:0];//延迟调用会卡住当前线程(只会卡子线程哦,主线程不会卡)
[[NSRunLoop currentRunLoop] run];//执行test
NSLog(@"3");
});
}
- (void)test{
NSLog(@"2");
}
2019-09-18 10:38:37.881686+0800 LGRunLoopTest[1431:396724] 1
2019-09-18 10:38:37.882088+0800 LGRunLoopTest[1431:396724] 2
2019-09-18 10:38:37.882203+0800 LGRunLoopTest[1431:396724] 3
2、代码实例2 子线程timer,[[NSRunLoop currentRunLoop] run];
后的代码能否执行
-
timer
运行当中,NSRunLoop
可以理解为不休眠,所以下面的NSLog(@"3");
没法执行 -
repeats
改为NO
,NSRunLoop
执行完time
,然后执行NSLog(@"3");
- 会卡住当前线程(只会卡子线程哦,主线程不会卡)
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
[super touchesBegan:touches withEvent:event];
dispatch_async(dispatch_get_global_queue(0, 0), ^{
NSLog(@"1");
NSTimer *timer = [NSTimer timerWithTimeInterval:1 target:self selector:@selector(test) userInfo:nil repeats:YES];
[[NSRunLoop currentRunLoop] addTimer:timer forMode:NSDefaultRunLoopMode];
[[NSRunLoop currentRunLoop] run];
NSLog(@"3");
});
}
- (void)test{
NSLog(@"2");
}
2019-09-18 10:48:56.215924+0800 LGRunLoopTest[1484:422074] 1
2019-09-18 10:48:57.220180+0800 LGRunLoopTest[1484:422074] 2
2019-09-18 10:48:58.219931+0800 LGRunLoopTest[1484:422074] 2
2019-09-18 10:48:59.220414+0800 LGRunLoopTest[1484:422074] 2
2019-09-18 10:49:00.216648+0800 LGRunLoopTest[1484:422074] 2
2019-09-18 10:49:01.218170+0800 LGRunLoopTest[1484:422074] 2
2019-09-18 10:49:02.219192+0800 LGRunLoopTest[1484:422074] 2
2019-09-18 10:49:03.220644+0800 LGRunLoopTest[1484:422074] 2
...
3、代码实例3 主线程timer,timer后的代码执行
- 主线程后面的执行,这个和子线程不一样。
- 会卡住当前线程(只会卡子线程哦,主线程不会卡)
-
NSRunLoop
运行循环,没有事情的时候休眠,可以理解为可以执行run
后面的代码;有事情的时候是个循环,没法执行run
后面的代码
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
[super touchesBegan:touches withEvent:event];
NSLog(@"1");
NSTimer *timer = [NSTimer timerWithTimeInterval:1 target:self selector:@selector(test) userInfo:nil repeats:YES];
[[NSRunLoop currentRunLoop] addTimer:timer forMode:NSDefaultRunLoopMode];
NSLog(@"3");
}
- (void)test{
NSLog(@"2");
}
2019-09-18 10:57:00.964758+0800 LGRunLoopTest[1497:444653] 1
2019-09-18 10:57:00.964953+0800 LGRunLoopTest[1497:444653] 3
2019-09-18 10:57:01.965108+0800 LGRunLoopTest[1497:444653] 2
2019-09-18 10:57:02.965569+0800 LGRunLoopTest[1497:444653] 2
2019-09-18 10:57:03.965910+0800 LGRunLoopTest[1497:444653] 2
2019-09-18 10:57:04.965903+0800 LGRunLoopTest[1497:444653] 2
...
网友评论