子線程裏調用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

相關文章
相關標籤/搜索