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

      

  • 相关阅读:
    springboot访问redis序列化问题
    观察者模式在源码中的使用
    抽象工厂模式在源码中的使用
    工厂方法模式在源码中的应用
    策略模式的应用
    springcloud实现限流
    Elasticsearch
    java8新特性(二)StreamApi
    thinkpaidE450 win10 进入bios
    restframe_work2
  • 原文地址:https://www.cnblogs.com/doudouyoutang/p/10889092.html
Copyright © 2011-2022 走看看