zoukankan      html  css  js  c++  java
  • 《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]]) {
                
            }
        });
    

      

  • 相关阅读:
    MVC中生成PDF并在web中显示
    Excel中如何避免删除未筛选得数据
    double数据类型四舍五入的bug
    Oracle 计算两个日期间时间排除非工作日及非工作时间精确至分钟
    数据库日志文件收缩
    Repeater 数据绑定 分页
    文件迁移 得到文件夹下所有文件名称
    共享业务稳定性测试&技术创新组
    产品测试组和业务测试组
    初级Bug率,随时受不了
  • 原文地址:https://www.cnblogs.com/doudouyoutang/p/10889092.html
Copyright © 2011-2022 走看看