dispatch_async(dispatch_get_global_queue(0, 0), ^{
NSLog(@"before perform");
[self performSelector:@selector(printLog) withObject:self afterDelay:0]; //延時(shí)這個(gè)方法不會(huì)被調(diào)用归榕,不會(huì)打印printLog
[self performSelector:@selector(printLog) withObject:nil]; //這個(gè)方法會(huì)調(diào)用,會(huì)打印printLog
NSLog(@"after perform");
});
}
- (void)printLog {
NSLog(@"printLog");
}
Why
結(jié)論
- 1.performSelector 如果不使用延時(shí),程序會(huì)再子線程上直接調(diào)用該方法铸题,方法會(huì)被調(diào)用
- 2.如果使用延時(shí),在子線程中方法不會(huì)被調(diào)用卸亮,因?yàn)樵摲椒ǖ却〞r(shí)器去調(diào)用短荐,而該子線程中沒有定時(shí)器混巧,所以不會(huì)調(diào)用
- 3.解決2的方法就是使用dispatch_after里面會(huì)有一個(gè)定時(shí)器,來調(diào)用方法
解決辦法
dispatch_async(dispatch_get_global_queue(0, 0), ^{
NSLog(@"before perform");
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(3 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
[self printLog];
});
NSLog(@"after perform");
});