《iOS面试之道》-勘误2

一、如何保证NSTimer不受Runloop的影响,准时触发

  书中提到两种方案,

  一种是改变timer加入到runloop中的Mode,为CommonModes不受Runloop的Mode影响

  第二种是下面图片中的方案,这个方案中的代码是存在问题的

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    [self testOne:1];
//    [self testOne:2];
//    [self testOne:3];
//    [self testOne:4];
//
//    [self testOne:5];
//    [self testOne:6];
//    [self testOne:7];
//    [self testOne:8];
}

- (void)testOne:(NSInteger)i
{
    dispatch_async(dispatch_get_global_queue(0, 0), ^{
        
        NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:1 repeats:YES block:^(NSTimer * _Nonnull timer) {
            [self tick:nil];
        }];
        
        [[NSRunLoop currentRunLoop] addTimer:timer forMode:NSRunLoopCommonModes];
        
        NSLog(@"%s", __FUNCTION__);
    });
}

- (void)tick:(NSTimer *)timer
{
    NSLog(@"%s", __FUNCTION__);
}


@end

  这个代码执行完后,定时器中的方法是不会触发的。

  因为这个线程已经结束,线程持有的Runloop也结束了。

  除非,这个线程不会结束,类似下面

  

    dispatch_async(dispatch_get_global_queue(0, 0), ^{
        
        NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:1 repeats:YES block:^(NSTimer * _Nonnull timer) {
            [self tick:nil];
        }];
        
        [[NSRunLoop currentRunLoop] addTimer:timer forMode:NSRunLoopCommonModes];
        
        NSLog(@"%s", __FUNCTION__);
        
        while ([[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate distantFuture]]) {
            
        }
    });

  

猜你喜欢

转载自www.cnblogs.com/doudouyoutang/p/10889092.html