在iOS中NSRunLoop是一個(gè)對(duì)象歼捏,run是它的一個(gè)對(duì)象方法,runloop嵌套一般指的是在NSRunLoop的run方法中再調(diào)用一次run方法,runloop的run方法內(nèi)部都是開(kāi)啟了一個(gè)while循環(huán),因此在run方法中再調(diào)用一次run方法則相當(dāng)于在一個(gè)while循環(huán)中開(kāi)啟另一個(gè)while循環(huán),內(nèi)層while循環(huán)會(huì)導(dǎo)致外層while循環(huán)的阻塞盟萨。
BOOL r1_run = YES;
BOOL r2_run = YES;
while (r1_run) {
NSLog(@"---1-");
while (r2_run) {
}
NSLog(@"---2-");
}
另外,在runloop 中了讨,每開(kāi)啟一個(gè)run方法都是在處理runloop 指定mode下的modeItem的事件捻激,如果外層run方法與內(nèi)層run方法運(yùn)行的是在同一mode下,那在外層run方法中沒(méi)來(lái)得及處理的modeItem的事件在進(jìn)入內(nèi)層run方法后可以繼續(xù)被處理前计。
1胞谭、練習(xí)一
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
NSTimer *timer1 = [NSTimer timerWithTimeInterval:5 target:self selector:@selector(timerMethod) userInfo:nil repeats:YES];
[[NSRunLoop currentRunLoop] addTimer:timer1 forMode:NSDefaultRunLoopMode];
}
- (void)timerMethod {
NSLog(@"---1");
[[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate distantFuture]];
NSLog(@"---2");
}
2、練習(xí)二:求下列代碼打印順序
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
dispatch_async(dispatch_get_main_queue(), ^{
NSLog(@"1");
});
[[NSRunLoop currentRunLoop] performBlock:^{
NSLog(@"2");
}];
NSTimer *timer = [[NSTimer alloc] initWithFireDate:[NSDate date] interval:10 repeats:YES block:^(NSTimer * _Nonnull timer) {
NSLog(@"3");
CFRunLoopStop(CFRunLoopGetCurrent());
}];
[[NSRunLoop currentRunLoop] addTimer:timer forMode:@"mode1"];
[[NSRunLoop currentRunLoop] runMode:@"mode1" beforeDate:[NSDate distantFuture]];
NSLog(@"4");
}
3残炮、練習(xí)三:求下列代碼打印內(nèi)容
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
NSThread *thread = [[NSThread alloc] initWithTarget:self selector:@selector(threadStartMethod) object:nil];
[thread start];
}
- (void)threadStartMethod {
NSTimer *timer1 = [NSTimer timerWithTimeInterval:5 target:self selector:@selector(timerMethod) userInfo:nil repeats:YES];
NSTimer *timer2 = [NSTimer timerWithTimeInterval:3 target:self selector:@selector(timerMethod2) userInfo:nil repeats:YES];
[[NSRunLoop currentRunLoop] addTimer:timer1 forMode:@"mode1"];
[[NSRunLoop currentRunLoop] addTimer:timer2 forMode:@"mode1"];
[[NSRunLoop currentRunLoop] runMode:@"mode1" beforeDate:[NSDate distantFuture]];
}
- (void)timerMethod {
NSLog(@"---1");
NSTimer *timer4 = [NSTimer timerWithTimeInterval:10 target:self selector:@selector(timerMethod4) userInfo:nil repeats:YES];
[[NSRunLoop currentRunLoop] addTimer:timer4 forMode:@"mode1"];
[[NSRunLoop currentRunLoop] runMode:@"mode1" beforeDate:[NSDate distantFuture]];
NSLog(@"---2");
}
- (void)timerMethod2 {
NSLog(@"---3");
NSTimer *timer3 = [NSTimer timerWithTimeInterval:10 target:self selector:@selector(timerMethod3) userInfo:nil repeats:YES];
[[NSRunLoop currentRunLoop] addTimer:timer3 forMode:@"mode1"];
[[NSRunLoop currentRunLoop] runMode:@"mode1" beforeDate:[NSDate distantFuture]];
NSLog(@"---4");
}
- (void)timerMethod3 {
NSLog(@"---5");
// CFRunLoopStop(CFRunLoopGetCurrent());
}
- (void)timerMethod4 {
NSLog(@"---6");
}