runloop 和 CFRunLoop - 定时器 - NSTimer 和 GCD定时器
1.

2、
#import "ViewController.h" @interface ViewController ()
@property (nonatomic, strong) dispatch_source_t timer;
@end @implementation ViewController -(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
[self gcdTimer];
} /**
GCD定时器
*/
-(void)gcdTimer{ /**
DISPATCH_SOURCE_TYPE_TIMER 定时器
0 描述信息
0 更详细的描述信息
dispatchQueue : 队列决定定时器任务在哪个线程
*/
// dispatch_source_t timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, dispatch_get_main_queue());
dispatch_source_t timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, , , dispatch_get_global_queue(, )); /*
DISPATCH_TIME_NOW :起始时间
intervalInSeconds 间隔时间
leewayInSeconds 精准度, 绝对精准0
*/
dispatch_source_set_timer(timer, DISPATCH_TIME_NOW, * NSEC_PER_SEC, * NSEC_PER_SEC); /*
定时器任务
*/
dispatch_source_set_event_handler(timer, ^{
NSLog(@"=== %@", [NSThread currentThread]);
});
dispatch_resume(timer); /*
此时 定时器不会工作, 因为定时器有可能被释放掉了,异步线程和主线程都不会执行 ,需要强引用, strong,
GCD定时器是绝对精准的,拖拽tableview或者textview都会执行,NSTimer 分界面追踪模式和默认模式
*/ self.timer = timer;
} -(void)runloopTimer{
NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval: target:self selector:@selector(runTimer) userInfo:nil repeats:YES];// 在主线程 // NSRunLoopCommonModes:会同时执行默认模式(NSDefaultRunLoopMode)和界面追踪模式(UITrackingRunLoopMode)
[[NSRunLoop currentRunLoop] addTimer:timer forMode:UITrackingRunLoopMode]; // 等价于 CFRunLoopAddTimer (参数1,参数2,参数3)
}
- (void)runloopTimer22{
NSRunLoop *runloop = [NSRunLoop currentRunLoop]; /**
默认是NSDefaultRunLoopMode
*/
[NSTimer scheduledTimerWithTimeInterval: target:self selector:@selector(runTimer) userInfo:nil repeats:YES];
[runloop run];
} -(void)runTimer{
NSLog(@"===%@====",[NSThread currentThread]);
} -(void)runLoop{
//主线程runloop
NSRunLoop *mainRunloop = [NSRunLoop mainRunLoop];
NSLog(@"mainRunloop =========== %p", mainRunloop);
// NSLog(@"mainRunloop 是个啥 === %@", mainRunloop);//打印一坨东西看不明白 //当前线程runloop
NSRunLoop * currentRunLoop = [NSRunLoop currentRunLoop];
NSLog(@"currentRunLoop ======== %p", currentRunLoop); //core
NSLog(@"CFRunLoopGetMain ====== %p", CFRunLoopGetMain());
NSLog(@"CFRunLoopGetCurrent === %p", CFRunLoopGetCurrent()); //转换
NSLog(@"mainRunloop.getCFRunLoop === %p", mainRunloop.getCFRunLoop);
NSLog(@"currentRunLoop .getCFRunLoop === %p", currentRunLoop.getCFRunLoop);
/*
2018-07-06 13:52:44.552563+0800 10 - runloop[12596:484429] mainRunloop =========== 0x6040000a2be0
2018-07-06 13:52:44.552908+0800 10 - runloop[12596:484429] currentRunLoop ======== 0x6040000a2be0
2018-07-06 13:52:44.553043+0800 10 - runloop[12596:484429] CFRunLoopGetMain ====== 0x6000001f3600
2018-07-06 13:52:44.553121+0800 10 - runloop[12596:484429] CFRunLoopGetCurrent === 0x6000001f3600
*/
} @end
runloop 和 CFRunLoop - 定时器 - NSTimer 和 GCD定时器的更多相关文章
- ios 中定时器:NSTimer, CADisplayLink, GCD
#import "ViewController.h" #import "RunloopViewController.h" @interface ViewCont ...
- IOS GCD定时器
提到定时器,NStimer肯定是我们最为熟悉的. 但是NStimer有着很大的缺点,并不准确. 通俗点说,就是它该做他的事了,但是由于其他事件的影响,Nstimer会放弃他应该做的. 而GCD定时器, ...
- Object-C定时器,封装GCD定时器的必要性!!! (一)
实际项目开发中经常会遇到延迟某件任务的执行,或者让某件任务周期性的执行.然后也会在某些时候需要取消掉之前延迟执行的任务. iOS中延迟操作有三种解决方案: 1.NSObject的方法:(对象方法) p ...
- ios基础篇(二十三)—— 定时器NSTimer与图片的自动切换
一.NSTimer NSTimer是一个能在从现在开始到后面的某一个时刻或者周期性的执行我们指定的方法的对象.可以按照一定的时间间隔,将制定的信息发送给目标对象.并更新某个对象的行为.你可以选择在未来 ...
- 定时器(NSTimer)
iOS中定时器NSTimer的使用 1.初始化 + (NSTimer *)timerWithTimeInterval:(NSTimeInterval)ti target:(id)aTarget sel ...
- iOS定时器NSTimer的使用方法
1.初始化 + (NSTimer *)timerWithTimeInterval:(NSTimeInterval)ti target:(id)aTarget selector:(SEL)aSelect ...
- 【转】iOS中定时器NSTimer的使用
原文网址:http://www.cnblogs.com/zhulin/archive/2012/02/02/2335866.html 1.初始化 + (NSTimer *)timerWithTimeI ...
- iOS中定时器NSTimer的使用-备用
1.初始化 + (NSTimer *)timerWithTimeInterval:(NSTimeInterval)ti target:(id)aTarget selector:(SEL)aSelect ...
- iOS定时器-- NSTimer 和CADisplaylink
iOS定时器-- NSTimer 和CADisplaylink 一.iOS中有两种不同的定时器: 1. NSTimer(时间间隔可以任意设定,最小0.1ms)// If seconds is les ...
随机推荐
- 浅析Spring框架之一(Spring简介)
免责声明 本文为鄙人搜集网络资源并结合自己所思所得整理而成,如有侵权,敬请谅解. 何为spring框架 Spring是一个开源的轻量级控制反转(IoC)和面向切面(AOP)的容器框架. ◆目的:解决企 ...
- Android RIL概述
前言 Android作为一个通用的移动平台,其首要的功能就是通话.短信以及上网等通信功能.那么,从系统的角度来看,Android究竟是怎么实现与网络的交互的了? 这篇文章里,就来看一看Android中 ...
- mac osx下虚拟主机配置
1.打开“终端(terminal)”,输入 sudo apachectl -v,此指令显示apache版本 2.开启apache,输入 sudo apachectl start ...
- 黏包的原理 (tcp udp) struct模块
黏包 指数据混乱问题(发送端发送数据,接收端不知如何去接收) 只有tcp协议才会发送粘包,udp不会发生 黏包(tcp) 有一个合包机制(nagle算法),将多次连续发送且间隔较小的数据,进行打包成一 ...
- SQL Server数据库定时备份解决方案
SQL Server数据库定时备份解决方案 1.本方案采用软件为:SQLBackupAndFTP 10.0.3 版本,压缩包自带注册机,请自行破解. 2.软件截图如下: 3.功能说明:自动定时备份相关 ...
- uva-657-搜索
注意是四个方向(上下左右),不是八个方向,当成了八个方向做,一直wa AC时间:0ms #include<stdio.h> #include<iostream> #includ ...
- 本地yum源快速创建
1.建立挂载目录mkdir /rui 2.挂载iso到新建的/rui目录
- 8. mybatis实战教程(mybatis in action)之七:实现mybatis分页(源码下载)
转自:https://blog.csdn.net/tangruyi1992/article/details/52584012 上 一篇文章里已经讲到了mybatis与spring MVC的集成,并且做 ...
- discuz 标签详解
Discuz 模板标签说明 Discuz! 的模板采用近似 PHP 表达式的语法,基本都是可识别的HTML,但涉及到变量和动态内容时,基本形式下: <!-{ 代码内容 }-> 逻辑元素包围 ...
- VBA 公式中使用相对位置
.Cells(3, 4).FormulaR1C1 = "=sum(r[-" & a & "]c[0]:r[-3]c[" & b & ...