您的位置:首页 > 其它

实现定时器功能的几种方式

2017-12-22 11:44 232 查看
nsrunLoop

GCD

RAC

NsrunLoop

NSRunLoop是IOS消息机制的处理模式

一条线程对应一个RunLoop,主线程的RunLoop默认已经创建好了, 而子线程的需要我们自己手动创建

获取主线程对应的RunLoop对象mainRunLoop/CFRunLoopGetMain( [NSRunLoop mainRunLoop]

获取当前线程对应的RunLoop对象currentRunLoop/CFRunLoopGetCurrent ( [NSRunLoop currentRunLoop]

RunLoop会一直循环检测,从线程start到线程end,检测检测到事件源(CFRunLoopSourceRef)执行处理函数,首先会产生通知,corefunction向线程添加runloopObservers来监听事件,并控制NSRunLoop里面线程的执行和休眠,在有事情做的时候使当前NSRunLoop控制的线程工作,没有事情做让当前NSRunLoop的控制的线程休眠。

-(void)demo1
{

//事件 交给谁处理?Runloop
NSTimer *timer =  [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(timeMethod) userInfo:nil repeats:YES];

[[NSRunLoop currentRunLoop]addTimer:timer forMode:NSRunLoopCommonModes];
}
//所有被"标记"common的模式都可以运行,UITrackingRunLoopMode和kCFRunLoopDefaultMode都被标记为了common模式,所以只需要将timer的模式设置为forMode:NSRunLoopCommonModes,就可以在默认模式和追踪模式都能够运行

-(void)timeMethod
{

NSLog(@"能不能执行!");

}


NsrunLoop的mode

NSDefaultRunLoopMode:App的默认Mode,通常主线程是在这个Mode下运行(UI事件会阻塞定时器的运行)

UITrackingRunLoopMode:界面跟踪Mode,用于ScrollView 追踪触摸滑动,保证界面滑动时不受其他 Mode 影响

UIInitializationRunLoopMode:在刚启动App时第进入的第一个 Mode,启动完成后就不再使用

GSEventReceiveRunLoopMode: 接受系统事件的内部 Mode,通常用不到

NSRunLoopCommonModes:这是一个占位用的Mode,不是一种真正的Mode

GCD

///将定时器设置在主线程
dispatch_source_t timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, dispatch_get_global_queue(0, 0));

//2设置定时器每一秒执行一次 GCD时间事件 1000000000
dispatch_source_set_timer(timer, DISPATCH_TIME_NOW, 1.0 * NSEC_PER_SEC, 0);
3设置定时器执行的动作
dispatch_source_set_event_handler(timer, ^{
NSLog(@"----%@",[NSThread currentThread]);
NSLog(@"执时钟事件");
});

//4.启动定时器
dispatch_resume(timer);
_timer = timer;//设置一个变量存储timer 否则执行一次就被释放了,是因为内存管理的原因,使用了dispatch_source_create方法,这种方法GCD是不会帮你管理内存的


dispatch_source_create

dispatch_source_create(dispatch_source_type_t type,
uintptr_t handle,
unsigned long mask,
dispatch_queue_t _Nullable queue)


第一个参数:dispatch_source_type_t type为设置GCD源方法的类型,这里设置类型为定时器

type类型

DISPATCH_SOURCE_TYPE_DATA_ADD 变量增加

DISPATCH_SOURCE_TYPE_DATA_OR 变量 OR

DISPATCH_SOURCE_TYPE_MACH_SEND MACH端口发送

DISPATCH_SOURCE_TYPE_MACH_RECV MACH端口接收

DISPATCH_SOURCE_TYPE_MEMORYPRESSURE 内存压力 (注:iOS8后可用)

DISPATCH_SOURCE_TYPE_PROC 检测到与进程相关的事件

DISPATCH_SOURCE_TYPE_READ 可读取文件映像

DISPATCH_SOURCE_TYPE_SIGNAL 接收信号

DISPATCH_SOURCE_TYPE_TIMER 定时器

DISPATCH_SOURCE_TYPE_VNODE 文件系统有变更

DISPATCH_SOURCE_TYPE_WRITE 可写入文件映像

第二个参数:uintptr_t handle Apple的API介绍说,暂时没有使用,传0即可。

第三个参数:unsigned long mask Apple的API介绍说,使用DISPATCH_TIMER_STRICT,会引起电量消耗加剧,毕竟要求精确时间,所以一般传0即可,视业务情况而定。

第四个参数:dispatch_queue_t _Nullable queue 队列,将定时器事件处理的Block提交到哪个队列之上。可以传Null,默认为全局队列。注意:当提交到全局队列的时候,时间处理的回调内,需要异步获取UI线程,更新UI

dispatch_source_set_timer

dispatch_source_set_timer(dispatch_source_t source,
dispatch_time_t start,
uint64_t interval,
uint64_t leeway);


第一个参数:dispatch_source_t source不用说了,传上一步的timer

第二个参数:dispatch_time_t start, 定时器开始时间,类型为 dispatch_time_t,其API的abstract标明可参照dispatch_time()和dispatch_walltime(),同为设置时间,但是后者为“钟表”时间,相对比较准确,所以选择使用后者。dispatch_walltime(const struct timespec *_Nullable when, int64_t delta),参数when可以为Null,默认为获取当前时间,参数delta为增量,即获取当前时间的基础上,增加X秒的时间为开始计时时间,此处传0即可。 我们一般用DISPATCH_TIME_NOW现在开始

第三个参数:uint64_t interval,定时器间隔时长,由业务需求而定。

第四个参数:uint64_t leeway, 允许误差,此处传0即可。

dispatch_source_set_event_handler

dispatch_source_set_event_handler(dispatch_source_t source,
dispatch_block_t _Nullable handler)


第一个参数:dispatch_source_t source不用说了,传上上一步的timer

第二个参数:dispatch_block_t _Nullable handler,定时器执行的动作,需要处理的业务逻辑Block。

dispatch_resume

dispatch_resume(_timer)


定时器创建完成并不会运行,需要主动去触发,也就是调用上述方法。

调度源提供了源事件的处理回调,同时也提供了取消源事件处理的回调,使用非常方便。

dispatch_source_set_cancel_handler(dispatch_source_t source,
dispatch_block_t _Nullable handler)


这个方法参数一看就明白了

RAC

RACDisposable* disble =   [[RACSignal interval:1.0 onScheduler:[RACScheduler scheduler]]subscribeNext:^(NSDate * _Nullable x) {

NSLog(@"%@",[NSThread currentThread]);

}];

[disble dispose];//取消  比如点击什么事件取消
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: