以下代码执行顺序是什么 ?
- (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]; NSLog(@"4"); }); NSLog(@"2"); } - (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 |