RunLoop

2020-05-21  本文已影响0人  Silence_xl
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    dispatch_async(dispatch_get_global_queue(0, 0), ^{
        NSLog(@"1");
        [self performSelector:@selector(test) withObject:nil afterDelay:0];
        NSLog(@"3");
    });
}

- (void) test{
    NSLog(@"2");
}

2020-05-21 15:54:30.507697+0800 Demo1[26980:1523527] 1
2020-05-21 15:54:30.507889+0800 Demo1[26980:1523527] 3

不会执行test方法主要是runloop没有开启。

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    dispatch_async(dispatch_get_global_queue(0, 0), ^{
        NSLog(@"1");
        [[NSRunLoop currentRunLoop] run];
        [self performSelector:@selector(test) withObject:nil afterDelay:0];
        NSLog(@"3");
    });
}

- (void) test{
    NSLog(@"2");
}

2020-05-21 16:08:35.589099+0800 Demo1[27547:1556104] 1
2020-05-21 16:08:35.589334+0800 Demo1[27547:1556104] 3
2020-05-21 16:08:36.183748+0800 Demo1[27547:1556104] 1
2020-05-21 16:08:36.184352+0800 Demo1[27547:1556104] 2
2020-05-21 16:08:36.184524+0800 Demo1[27547:1556104] 3

1、全局变量保存各个线程和各个 RunLoop 对象的关系,初始化的时候创建一个主线程的 RunLoop 。
2、子线程的 RunLoop 获取的时候开始创建。
3、RunLoop 的生命周期和线程的生命周期有关系!

Runloop
基本作用
Runloop与线程
获取runloop对象

Fundation

core fundation

RunLoop整体逻辑
image.png
image.png
上一篇 下一篇

猜你喜欢

热点阅读