美文网首页
runloop嵌套理解

runloop嵌套理解

作者: nunca | 来源:发表于2018-09-12 18:27 被阅读0次

在iOS中NSRunLoop是一个对象,run是它的一个对象方法,runloop嵌套一般指的是在NSRunLoop的run方法中再调用一次run方法,runloop的run方法内部都是开启了一个while循环,因此在run方法中再调用一次run方法则相当于在一个while循环中开启另一个while循环,内层while循环会导致外层while循环的阻塞。

    BOOL r1_run = YES;
    BOOL r2_run = YES;

    while (r1_run) {
        NSLog(@"---1-");
        while (r2_run) {
            
        }
        NSLog(@"---2-");
    }

另外,在runloop 中,每开启一个run方法都是在处理runloop 指定mode下的modeItem的事件,如果外层run方法与内层run方法运行的是在同一mode下,那在外层run方法中没来得及处理的modeItem的事件在进入内层run方法后可以继续被处理。

1、练习一


- (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、练习二:求下列代码打印顺序

- (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、练习三:求下列代码打印内容

- (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");
}

相关文章

  • runloop嵌套理解

    在iOS中NSRunLoop是一个对象,run是它的一个对象方法,runloop嵌套一般指的是在NSRunLoop...

  • Runloop分析

    RunLoop 参考:深入理解RunLoop ibireme:《深入理解RunLoop》 Runloop 的概念 ...

  • NSRunLoop

    深入理解RunLoop RunLoop深度探究(一) RunLoop深度探究(二) RunLoop深度探究(三) ...

  • iOS知识点(10)RunLoop

    深入理解RunLoop iOS---实例化讲解RunLoop iOS runloop iOS-RunLoop充满灵...

  • iOS 各种技术点网址

    RunLoop 深入理解RunLoop(作者 :ibireme)iOS线下分享《RunLoop》by 孙源@sun...

  • Runloop 详解

    Runloop 详解 参考链接: 深入理解RunLoop CFRunLoop 概念 runloop :是管理和处理...

  • 深入理解runloop

    原文 深入理解runloop 深入理解RunLoop 由 ibireme | 2015-05-18 | iOS, ...

  • iOS 这是你想要的

    Runloop 相关 Runloop 保持线程能随时处理事件而不退出深入理解Runloop 线程和Runloop ...

  • iOS开发基础之Runloop

    文章内容来自:深入理解RunLoop 官方文档-Runloop Runloop概念 Runloop本身是一个对...

  • OC部分原理参考

    1.RunLoop 深入理解RunLoopRunLoop 视频RunLoop第一讲 视频RunLoop实战 视频 ...

网友评论

      本文标题:runloop嵌套理解

      本文链接:https://www.haomeiwen.com/subject/naiygftx.html