• 《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]]) {
                
            }
        });
    

      

  • 相关阅读:
    学习FPGA的几个阶段
    Quartus 编译问题Info (176311): Pin ~ALTERA_nCEO~ is assigned to pin location Pin_P28 (IOPAD_X115_Y43_N7)
    No Title
    最近比较忙
    如何在Qsys 中定制Nand_Flash软核(学习)
    Nand_Flash工作原理(学习)2
    (转)FPGA时序约束的几种方法
    ‘Downloading ELF Process failed’问题如何解决
    celery使用的时候的坑
    Git命令一览
  • 原文地址:https://www.cnblogs.com/doudouyoutang/p/10889092.html
Copyright © 2020-2023  润新知