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 ...
随机推荐
- html_常用技巧总结
============= 博客大全: 脚本之家:http://www.jb51.net/list/list_233_104.htm 红黑联盟: http://www.2cto.com/kf/yid ...
- 3种方法轻松处理php开发中emoji表情的问题
背景 做微信开发的时候就会发现,存储微信昵称必不可少. 可这万恶的微信支持emoji表情做昵称,这就有点蛋疼了 一般Mysql表设计时,都是用UTF8字符集的.把带有emoji的昵称字段往里面inse ...
- 开发组件:ZeroMQ
ZeroMQ https://blog.csdn.net/w174504744/article/details/73187697
- Python分页转Mybatis pagehelper格式分页
最近工作里遇到一个需求要把之前用Java写的一个http接口替换成用Python写的,出参是带了mybatis pageHelper中PageInfo信息的一个JSON串,而Python这边分页不会涉 ...
- shell语法(二)
Shell脚本语法 条件测试:test. [ ] 命令test或[可以测试一个条件是否成立,如果测试结果为真,则该命令的Exit Status为0,如果测试结果为假,则命令的Exit Status为1 ...
- loganalyzer
- 1.纯 CSS 创作一个按钮文字滑动特效 + 弹幕(残缺)
原文地址:1# 视频演示如何用纯 CSS 创作一个按钮文字滑动特效 扩展后地址:https://scrimba.com/c/cJkzMfd HTML代码: <html> <head& ...
- 普通web项目转maven项目
先要有pom.xml文件 1.首先你要确定你的开发工具是否已经安装上maven: 2.安装配置好后将你的项目导入到开发工具上: 3.右键点击java项目,选择maven选项,在选择Enable Dep ...
- java垃圾回收几种算法
1.引用计数法 2.标记——清除法 3.标记——整理算法 4.copying算法 5.generation算法(新生代.老年代.持久代) 详情参考:深入理解 Java 垃圾回收机制
- 在 WampServer 上手工安装 PHP 的多个版本
手工安装新版本的PHP,只需以下步骤: 下载要安装的PHP版本.既然是用WampServer,那当然是下载Window版本的ZIP包啦:http://windows.php.net.解压到 Wamp的 ...