代码同步到主线程执行的三种方法如下:
// 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!";
});
网友评论