记录一下前几天的学习内容
网上有两篇文章写的很好
第一篇
Runloop的执行流程
1 即将进入Runloop:Rntry
2.do…while (线程保活) 被 for Ti 么。beforeSource0
3处理Blocks
4处理Source0
5再一次出炉Blocks
6 如果主主线程,GDC事件是否需要处理
7通知到Observer: beforeWaiting
8等待唤醒
9通知Observer,AfterWaiting
10被什么唤醒就处理什么事件
//
// ViewController.m
// Runloop
//
// Created by Civet on 2020/6/28.
// Copyright © 2020年 icivet. All rights reserved.
//
//https://www.jianshu.com/p/ac05ac8428ac
//https://www.jianshu.com/p/fcb271f69038
// 默认模式 UI模式
// observer observer
// Source Source
// Timer Timer
//Runloop — 运行循环
//是死循环,目的
//保证程序(runloop所在线程)不退出
//负责监听iOS事件 如:触摸 时钟,网络
//
//Source:事件源
//按照函数调用栈
//Source0:非Source1
//Source1:系统内核事件
#import "ViewController.h"
@interface ViewController ()
//tableView加载高清大图优化
typedef void(^RunloopBlock)(void);
@property (nonatomic,strong) NSMutableArray *taskes;
@property (nonatomic, assign) BOOL finished;
@property (nonatomic,strong) dispatch_source_t timer;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
//tableViewCell里每一个cell要加载三张高清大图的时候 滑动就会卡 监听Runloop循环 让runloop循环一次加载一张!
// [self addRunloopObserver];
// NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:0.000001 target:self selector:@selector(timerMethod) userInfo:nil repeats:YES];
//添加个timer 就可以使runloop一直工作不休息
_finished = NO;
NSThread * thread = [[NSThread alloc]initWithBlock:^{
NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(timerMethod) userInfo:nil repeats:YES];
/*
NSDefaultRunLoopMode 默认模式 一般处理timer、网络等事件
UITrackingRunLoopMode UI模式 优先级最高 但只有触摸事件才能触发 专门处理UI事件
把
NSRunLoopCommonModes 占位模式 (UI &&默认)
*/
[[NSRunLoop currentRunLoop] addTimer:timer forMode:NSDefaultRunLoopMode];
// [[NSRunLoop currentRunLoop] addTimer:timer forMode:NSRunLoopCommonModes];
[[NSRunLoop currentRunLoop] run];//死循环
//每条线程都有自己的runloop,但默认不开启循环! 当不写这句话时 1执行2不执行
//写这句时 2执行1不执行
while (!self.finished) {
[[NSRunLoop currentRunLoop] runUntilDate:[NSDate dateWithTimeIntervalSinceNow:0.000001]];
}
NSLog(@"来了%@",[NSThread currentThread]);//代号1
}];
[thread start];
///
//GDC里的定时器
// self.timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, dispatch_get_global_queue(0, 0));
// dispatch_source_set_timer(self.timer , DISPATCH_TIME_NOW, 1.0 * NSEC_PER_SEC, 0 * NSEC_PER_SEC);
// dispatch_source_set_event_handler(self.timer , ^{
// NSLog(@"111%@", [NSThread currentThread]);
// });
// dispatch_resume(self.timer );
//
//
// dispatch_source_t timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, <#dispatchQueue#>);
// dispatch_source_set_timer(timer, DISPATCH_TIME_NOW, <#intervalInSeconds#> * NSEC_PER_SEC, <#leewayInSeconds#> * NSEC_PER_SEC);
// dispatch_source_set_event_handler(timer, ^{
// <#code to be executed when timer fires#>
// });
// dispatch_resume(timer);
}
-(void)timerMethod{
NSLog(@"come here");//代号2
[NSThread sleepForTimeInterval:1.0];
NSLog(@"%@",[NSThread currentThread]);
}
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
_finished = YES;
}
//添加观察者
-(void)addRunloopObserver{
CFRunLoopRef runloop = CFRunLoopGetCurrent();
static CFRunLoopObserverRef defaultModeObserver;
defaultModeObserver = CFRunLoopObserverCreate(NULL, kCFRunLoopBeforeWaiting, YES, 0, &Callback, nil);
CFRunLoopAddObserver(runloop, defaultModeObserver, kCFRunLoopDefaultMode);
}
//这是C 语言的函数
static void Callback(){
NSLog(@"laile");
//tableViewCell里每一个cell要加载三张高清大图的时候 滑动就会卡 监听Runloop循环 让runloop循环一次加载一张!
}
//cell方法里➕
//[self addTask:^{
// //添加图片[ViewController addImageWith:cell];
//}];
//[self addTask:^{
// //添加图片[ViewController addImageWith:cell];
//}];
//[self addTask:^{
// //添加图片[ViewController addImageWith:cell];
//}];
-(void)addTask:(RunloopBlock)task{
[self.taskes addObject:task];
if (self.taskes.count > 18) {
[self.taskes removeObjectAtIndex:0];
}
}
@end