子线程开启定时器的两种办法

方法一

   dispatch_async(dispatch_get_global_queue(0, 0), ^{
       self.timer = [NSTimer timerWithTimeInterval:3 repeats:YES block:^(NSTimer * _Nonnull timer) {
   NSLog(@"定时器启动了 %@", [NSThread currentThread] );
        }];
        [[NSRunLoop currentRunLoop] addTimer:self.timer forMode:NSDefaultRunLoopMode];
        [[NSRunLoop currentRunLoop] run];    
    });

方法二

  dispatch_async(dispatch_get_global_queue(0, 0), ^{
    // 开启子线程
       self.timer = [NSTimer scheduledTimerWithTimeInterval:6 repeats:YES block:^(NSTimer * _Nonnull timer) {
           NSLog(@"定时器启动了 %@", [NSThread currentThread] );

       }];
        [self.timer fire]; //需要立即执行
       [[NSRunLoop currentRunLoop] run];  // 调用子线程runLoop 
    }); 

你可能感兴趣的:(基础OC)