iOS macOS 回到主线程的三种方式
简单说将代码同步到主线程执行的三种方法如下:
// 1.NSThread [self performSelectorOnMainThread:@selector(updateUI) withObject:nil waitUntilDone:NO]; - (void)updateUI { // UI更新代码 self.alert.text = @"Thanks!"; } // 2.NSOperationQueue [[NSOperationQueue mainQueue] addOperationWithBlock:^{ // UI更新代码 self.alert.text = @"Thanks!"; }]; // 3.GCD dispatch_async(dispatch_get_main_queue(), ^{ // UI更新代码 self.alert.text = @"Thanks!"; });
感谢:奋拓达 https://www.jianshu.com/p/c1f96d734616