美文网首页
RAC框架源码解析之NSTimer

RAC框架源码解析之NSTimer

作者: Jimi | 来源:发表于2018-04-22 23:38 被阅读60次

1、RunLoop基础知识

首先我们先简单的来说一下,我们都知道程序启动的时候去走进main函数,在mainAppDelegate作为入口并遵守代理,这样就会进入AppDelegate的代理方法中,如didFinishLaunchingWithOptions等方法。当走进main函数的时候,系统默认会开启一个运行循环并且是死循环也就是我们这节要说的RunLoop

那RunLoop它干了什么事?
1、负责监听事件(网络、时钟、触摸等)
2、如果没有事件发生,它会进入休眠状态
3、渲染界面,在一次循环中进行所有的渲染

1、NSTimer

NSTimer是苹果官方封装的一个倒计时类,使用起来比较简单。

//
//  ViewController.m
//  RAC_Timer
//
//  Created by JM on 2018/4/21.
//  Copyright © 2018年 JM. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(timerDown) userInfo:nil repeats:YES];
}

- (void)timerDown {
    static int i = 0;
    i++;
    NSLog(@"%d", i);
}

@end

控制台打印结果

2018-04-21 23:42:12.800474+0800 RAC_Timer[49953:2092718] 1
2018-04-21 23:42:13.801108+0800 RAC_Timer[49953:2092718] 2
2018-04-21 23:42:14.800973+0800 RAC_Timer[49953:2092718] 3
2018-04-21 23:42:15.800927+0800 RAC_Timer[49953:2092718] 4
2018-04-21 23:42:16.800965+0800 RAC_Timer[49953:2092718] 5
2018-04-21 23:42:17.800378+0800 RAC_Timer[49953:2092718] 6
2018-04-21 23:42:18.800847+0800 RAC_Timer[49953:2092718] 7
2018-04-21 23:42:19.799809+0800 RAC_Timer[49953:2092718] 8
2018-04-21 23:42:20.800715+0800 RAC_Timer[49953:2092718] 9
2018-04-21 23:42:21.799573+0800 RAC_Timer[49953:2092718] 10

2、NSTimer(RunLoop)

下面我们用RunLoop的方式来添加一下

下面有一个侧重点
1、NSDefaultRunLoopMode 为默认模式
2、UITrackingRunLoopMode 为UI模式,也就是说如果你创建该类型的RunLoop,它就只能被UI事件唤醒
3、NSRunLoopCommonModes 为占位模式,也就是说只要设置了该模式它就会同时通知默认模式以及UI模式

//
//  ViewController.m
//  RAC_Timer
//
//  Created by JM on 2018/4/21.
//  Copyright © 2018年 JM. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
//    [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(timerDown) userInfo:nil repeats:YES];

    NSTimer *timer = [NSTimer timerWithTimeInterval:1.0 target:self selector:@selector(timerDown) userInfo:nil repeats:YES];
    [[NSRunLoop currentRunLoop] addTimer:timer forMode:NSDefaultRunLoopMode];
}

- (void)timerDown {
    static int i = 0;
    i++;
    NSLog(@"%d", i);
}

@end

控制台打印结果

2018-04-22 01:19:16.039742+0800 RAC_Timer[51955:2174228] 1
2018-04-22 01:19:17.039835+0800 RAC_Timer[51955:2174228] 2
2018-04-22 01:19:18.039767+0800 RAC_Timer[51955:2174228] 3
2018-04-22 01:19:19.039715+0800 RAC_Timer[51955:2174228] 4
2018-04-22 01:19:20.039490+0800 RAC_Timer[51955:2174228] 5
2018-04-22 01:19:21.038807+0800 RAC_Timer[51955:2174228] 6
2018-04-22 01:19:22.039854+0800 RAC_Timer[51955:2174228] 7
2018-04-22 01:19:23.038787+0800 RAC_Timer[51955:2174228] 8
2018-04-22 01:19:24.039777+0800 RAC_Timer[51955:2174228] 9
2018-04-22 01:19:25.039236+0800 RAC_Timer[51955:2174228] 10

在开发中我们常常会把耗时操作放到子线程,像定时器这种东西我们也一般都是放在子线程的,但是会发现NSTimer在子线程中无效,我们对创建定时器的代码做如下修改:

    //创建子线程
    NSThread *thread = [[NSThread alloc] initWithBlock:^{
        NSLog(@"%@",[NSThread currentThread]);
        NSTimer *timer = [NSTimer timerWithTimeInterval:1.0 target:self selector:@selector(timerDown) userInfo:nil repeats:YES];
        [[NSRunLoop currentRunLoop] addTimer:timer forMode:NSDefaultRunLoopMode];
    }];
    
    //启动线程
    [thread start];

控制台打印

2018-04-22 01:32:10.741969+0800 RAC_Timer[52318:2191101] <NSThread: 0x60400046e740>{number = 3, name = (null)}

在上述代码中我们发现并没有执行定时器方法,这是为什么呢!因为在子线程代码块结束的时候该线程被销毁了,所以就不会进入定时器方法也就是timerDown中。

那么该如何解决?
在上面的代码中我们发现有把定时器放在RunLoop中,但为什么还是失效呢!因为手动创建RunLoop默认是不运行的,我们需要运行RunLoop,如下

    //创建子线程
    NSThread *thread = [[NSThread alloc] initWithBlock:^{
        NSLog(@"%@",[NSThread currentThread]);
        NSTimer *timer = [NSTimer timerWithTimeInterval:1.0 target:self selector:@selector(timerDown) userInfo:nil repeats:YES];
        [[NSRunLoop currentRunLoop] addTimer:timer forMode:NSDefaultRunLoopMode];
        //开启RunLoop循环
        [[NSRunLoop currentRunLoop] run];
    }];
    
    //启动线程
    [thread start];

控制台打印

2018-04-22 01:41:14.998636+0800 RAC_Timer[52531:2203640] <NSThread: 0x604000272040>{number = 3, name = (null)}
2018-04-22 01:41:16.002548+0800 RAC_Timer[52531:2203640] 1
2018-04-22 01:41:17.000635+0800 RAC_Timer[52531:2203640] 2
2018-04-22 01:41:18.000488+0800 RAC_Timer[52531:2203640] 3
2018-04-22 01:41:19.001231+0800 RAC_Timer[52531:2203640] 4
2018-04-22 01:41:20.003273+0800 RAC_Timer[52531:2203640] 5
2018-04-22 01:41:21.001323+0800 RAC_Timer[52531:2203640] 6
2018-04-22 01:41:22.001346+0800 RAC_Timer[52531:2203640] 7
2018-04-22 01:41:23.001389+0800 RAC_Timer[52531:2203640] 8
2018-04-22 01:41:24.004123+0800 RAC_Timer[52531:2203640] 9
2018-04-22 01:41:25.001335+0800 RAC_Timer[52531:2203640] 10

3、GCD实现倒计时

//
//  ViewController.m
//  RAC_Timer
//
//  Created by JM on 2018/4/21.
//  Copyright © 2018年 JM. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()

/** timer */
@property (nonatomic, strong) dispatch_source_t timer;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    //GCD设置timer
    dispatch_source_t timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, dispatch_get_global_queue(0, 0));
    
    //GCD时间
    dispatch_source_set_timer(timer, DISPATCH_TIME_NOW, 1.0 * NSEC_PER_SEC, 0);
    
    dispatch_source_set_event_handler(timer, ^{
        NSLog(@"%@", [NSThread currentThread]);
    });
    
    //启动
    dispatch_resume(timer);
    
    _timer = timer;

}
@end

控制台打印

2018-04-22 23:33:26.233998+0800 RAC_Timer[57174:2326547] <NSThread: 0x600000467180>{number = 3, name = (null)}
2018-04-22 23:33:27.234961+0800 RAC_Timer[57174:2326550] <NSThread: 0x604000467680>{number = 4, name = (null)}
2018-04-22 23:33:28.234041+0800 RAC_Timer[57174:2326550] <NSThread: 0x604000467680>{number = 4, name = (null)}
2018-04-22 23:33:29.234098+0800 RAC_Timer[57174:2326550] <NSThread: 0x604000467680>{number = 4, name = (null)}
2018-04-22 23:33:30.234248+0800 RAC_Timer[57174:2326550] <NSThread: 0x604000467680>{number = 4, name = (null)}

4、RAC实现倒计时

//rac实现倒计时
    [[RACSignal interval:1.0 onScheduler:[RACScheduler scheduler]] subscribeNext:^(NSDate * _Nullable x) {
        NSLog(@"%@", [NSThread currentThread]);
    }]

控制台打印

2018-04-22 23:33:26.233998+0800 RAC_Timer[57174:2326547] <NSThread: 0x600000467180>{number = 3, name = (null)}
2018-04-22 23:33:27.234961+0800 RAC_Timer[57174:2326550] <NSThread: 0x604000467680>{number = 4, name = (null)}
2018-04-22 23:33:28.234041+0800 RAC_Timer[57174:2326550] <NSThread: 0x604000467680>{number = 4, name = (null)}
2018-04-22 23:33:29.234098+0800 RAC_Timer[57174:2326550] <NSThread: 0x604000467680>{number = 4, name = (null)}
2018-04-22 23:33:30.234248+0800 RAC_Timer[57174:2326550] <NSThread: 0x604000467680>{number = 4, name = (null)}

demo源代码已放置GitHub地址https://github.com/JunAILiang/RAC_Demo

联系我:
qq: 1245424073
微信: liujunmin6980

相关文章

网友评论

      本文标题:RAC框架源码解析之NSTimer

      本文链接:https://www.haomeiwen.com/subject/totqlftx.html