- (void)performSelectorOnMainThread:(SEL)aSelector withObject:(nullable id)arg waitUntilDone:(BOOL)wait

一个例子解释这个方法的意思
performSelectorOnMainThread:回到主线程
waitUntilDone:这个意思看下面的例子

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    
    dispatch_async(dispatch_get_global_queue(0, 0), ^{
       
        NSLog(@"线程1 start");
        
        /**
         回到主线程
         waitUntilDone:YES:必须执行完主线程才能往下走
                         NO:不需要执行完主线程,可以先往下走
         */
        [self performSelectorOnMainThread:@selector(test1) withObject:nil waitUntilDone:NO];
        NSLog(@"线程1 end");
    });
}

- (void)test1 {
    
    sleep(1);
    NSLog(@"主线程 结束");
}

当 waitUntilDone:NO
打印的结果:
2017-02-16 11:16:33.837 Test[1189:30481] 线程1 start
2017-02-16 11:16:33.838 Test[1189:30481] 线程1 end
2017-02-16 11:16:34.872 Test[1189:30426] 主线程 tes1
当 waitUntilDone:YES
2017-02-16 11:21:29.292 Test[1242:32960] 线程1 start
2017-02-16 11:21:30.364 Test[1242:32922] 主线程 结束
2017-02-16 11:21:30.365 Test[1242:32960] 线程1 end

一幕了然,其实当waitUntilDone:YES ,是用到了 RunLoop的知识,让其一直在等待 直到完成,才往下走。

你可能感兴趣的:(- (void)performSelectorOnMainThread:(SEL)aSelector withObject:(nullable id)arg waitUntilDone:(BOOL)wait)