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定时器的更多相关文章

  1. ios 中定时器:NSTimer, CADisplayLink, GCD

    #import "ViewController.h" #import "RunloopViewController.h" @interface ViewCont ...

  2. IOS GCD定时器

    提到定时器,NStimer肯定是我们最为熟悉的. 但是NStimer有着很大的缺点,并不准确. 通俗点说,就是它该做他的事了,但是由于其他事件的影响,Nstimer会放弃他应该做的. 而GCD定时器, ...

  3. Object-C定时器,封装GCD定时器的必要性!!! (一)

    实际项目开发中经常会遇到延迟某件任务的执行,或者让某件任务周期性的执行.然后也会在某些时候需要取消掉之前延迟执行的任务. iOS中延迟操作有三种解决方案: 1.NSObject的方法:(对象方法) p ...

  4. ios基础篇(二十三)—— 定时器NSTimer与图片的自动切换

    一.NSTimer NSTimer是一个能在从现在开始到后面的某一个时刻或者周期性的执行我们指定的方法的对象.可以按照一定的时间间隔,将制定的信息发送给目标对象.并更新某个对象的行为.你可以选择在未来 ...

  5. 定时器(NSTimer)

    iOS中定时器NSTimer的使用 1.初始化 + (NSTimer *)timerWithTimeInterval:(NSTimeInterval)ti target:(id)aTarget sel ...

  6. iOS定时器NSTimer的使用方法

    1.初始化 + (NSTimer *)timerWithTimeInterval:(NSTimeInterval)ti target:(id)aTarget selector:(SEL)aSelect ...

  7. 【转】iOS中定时器NSTimer的使用

    原文网址:http://www.cnblogs.com/zhulin/archive/2012/02/02/2335866.html 1.初始化 + (NSTimer *)timerWithTimeI ...

  8. iOS中定时器NSTimer的使用-备用

    1.初始化 + (NSTimer *)timerWithTimeInterval:(NSTimeInterval)ti target:(id)aTarget selector:(SEL)aSelect ...

  9. iOS定时器-- NSTimer 和CADisplaylink

    iOS定时器-- NSTimer 和CADisplaylink 一.iOS中有两种不同的定时器: 1.  NSTimer(时间间隔可以任意设定,最小0.1ms)// If seconds is les ...

随机推荐

  1. [UE4]通过代码改变材质

    OrangeMaterial = ConstructorStatics.OrangeMaterial.Get(); , OrangeMaterial); 使用到的结构体如下: struct FCons ...

  2. 关于oracle数据库压力测试

    今天接到需求,需要对oracle数据库进行压力测试,就这几个字的需求. 然后查看了以下软件: 1.Benchmark Factory是一款专业的服务器性能测试工具,专为数据库测试和可扩展性测量而设计, ...

  3. 《opencv学习》 之 特征检测与匹配

    这几天学习SURF特征检测,直接看的视频和书本有点吃不消,现在是基本看懂了,如果写博客记录没有必要,因为网上都差不多,笔记都在书上了,以下是个人认为比较浅显易懂的文章,当然海有很多好文章我没看到. 看 ...

  4. 什么是 web 开发

    什么是 web 开发     这几天因为工作需要,了解了一下Web development 的技术路线,来源自     en.wikipedia.org/wiki/Web_development    ...

  5. 0_Simple__simpleSeparateCompilation

    ▶ 简单的将纯 C/C++ 函数放到另一个文件中,利用头文件引用到主体 .cu 中来,编译时共同编译. ▶ 源代码,把 C++ 的部分去掉了 // simpleDeviceLibrary.cuh #i ...

  6. jpa-入门级测试

  7. Node NPM 的常用配置

    1,修改 npm 下载模块的 保存地址 <1>  进入 cmd 运行, 如下命令 npm config set prefix  "C:\Program File\NodeJs\p ...

  8. Hadoop 执行 hdfs 命令烦人的警告信息

    问题描述: 安装完 hadoop,执行 hdfs dfs -ls 命令时,报警告如下: -- ::, WARN util.NativeCodeLoader: Unable to load native ...

  9. Overcoming Life's Obstacles - ASC 2017 March 03-04

    <Overcoming Life's Obstacles - How a top oboist overcame a neurological disorder to play again> ...

  10. Jquery和Ajax

    jQuery 是一个 JavaScript 函数库.JavaScript 是 HTML5 以及所有现代浏览器中的默认脚本语言! jQuery 库包含以下特性: HTML 元素选取 HTML 元素操作 ...