runloop嵌套理解

在iOS中NSRunLoop是一個(gè)對象,run是它的一個(gè)對象方法,runloop嵌套一般指的是在NSRunLoop的run方法中再調(diào)用一次run方法,runloop的run方法內(nèi)部都是開啟了一個(gè)while循環(huán),因此在run方法中再調(diào)用一次run方法則相當(dāng)于在一個(gè)while循環(huán)中開啟另一個(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 中,每開啟一個(gè)run方法都是在處理runloop 指定mode下的modeItem的事件,如果外層run方法與內(nèi)層run方法運(yùn)行的是在同一mode下,那在外層run方法中沒來得及處理的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");
}
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容