子线程中 [self performSelector:@sel

2018-02-23  本文已影响9人  二先生Developer
    dispatch_async(dispatch_get_global_queue(0, 0), ^{
        NSLog(@"before perform");
        [self performSelector:@selector(printLog) withObject:self afterDelay:0]; //延时这个方法不会被调用,不会打印printLog
        [self performSelector:@selector(printLog) withObject:nil]; //这个方法会调用,会打印printLog
        NSLog(@"after perform");
    });
}
- (void)printLog {
    NSLog(@"printLog");
}

Why
结论

解决办法

  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");
    });
上一篇 下一篇

猜你喜欢

热点阅读