子线程里调用performSelector须要注意什么

如下代码执行顺序是什么 ?async

- (void)action {oop

    NSLog(@"1");spa

    dispatch_queue_t queue = dispatch_get_global_queue(0, 0);线程

    dispatch_async(queue, ^{orm

        NSLog(@"3");get

        [self performSelector:@selector(test) withObject:nil afterDelay:0.0f];it

        NSLog(@"4");io

    });table

    NSLog(@"2");form

}

 

- (void)test {

    NSLog(@"5");

}

 

结果

2019-07-02 09:24:18.492671+0800 ModelTest[12945:510979] 1

2019-07-02 09:24:18.492776+0800 ModelTest[12945:510979] 2

2019-07-02 09:24:18.492807+0800 ModelTest[12945:511386] 3

2019-07-02 09:24:18.493009+0800 ModelTest[12945:511386] 4

 

test方法没有调用。由于子线程里runloop默认是关闭的。改成一下代码后

- (void)action {

    NSLog(@"1");

    dispatch_queue_t queue = dispatch_get_global_queue(0, 0);

    dispatch_async(queue, ^{

        NSLog(@"3");

        [self performSelector:@selector(test) withObject:nil afterDelay:0.0f];

        [[NSRunLoop currentRunLoop]run];

        NSLog(@"4");

    });

    NSLog(@"2");

}

 

- (void)test {

    NSLog(@"5");

}

结果

2019-07-02 09:38:40.874213+0800 ModelTest[13094:524707] 1

2019-07-02 09:38:40.874530+0800 ModelTest[13094:524707] 2

2019-07-02 09:38:40.874551+0800 ModelTest[13094:524802] 3

2019-07-02 09:38:43.792863+0800 ModelTest[13094:524802] 5

2019-07-02 09:38:43.793109+0800 ModelTest[13094:524802] 4

相关文章
相关标签/搜索