平時開發 用不到
保證我們的程序不退出
負責監聽事件,比如說觸摸、時鐘、網絡、更新我們的UI
如果沒有事件的發生,程序就會休眠
區分模式
FOUNDATION_EXPORT NSRunLoopMode const NSDefaultRunLoopMode;
FOUNDATION_EXPORT NSRunLoopMode const NSRunLoopCommonModes NS_AVAILABLE(10_5, 2_0);
第一種 : 時鐘模式、網絡模式
第二種: UI模式(用戶交互模式) 優先級最高
NSTimer Demo
- 寫一個Timer加入Runloop
- 在當前屏幕中添加一個TextView
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
NSTimer *timer = [NSTimer timerWithTimeInterval:1.0 target:self selector:@selector(updateTimer) userInfo:nil repeats:YES];
// 拿到我們的當前的Runloop 加入到默認的運行循環中
[[NSRunLoop currentRunLoop] addTimer:timer forMode:(NSDefaultRunLoopMode)];
}
// 我們來簡單打印一下當前線程
- (void)updateTimer{
NSLog(@"%@",[NSThread currentThread]);
}
第二種
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
// NSTimer *timer = [NSTimer timerWithTimeInterval:1.0 target:self selector:@selector(updateTimer) userInfo:nil repeats:YES];
// [[NSRunLoop currentRunLoop] addTimer:timer forMode:(NSDefaultRunLoopMode)];
NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(updateTimer) userInfo:nil repeats:YES];
}
- (void)updateTimer{
NSLog(@"%@",[NSThread currentThread]);
}
我們在Storyboard中拖一個TextView,拖動的時候,就發現,Timer不走了。
運行 拖動之后 發現 Timer打印不走了。
原因:Timer所在的Runloop和UI刷新的Runloop用的不是一個Runloop,而UI的Runloop模式的優先級呢比較高,所以會造成這種UI刷新優先級高造成Timer停頓的現象。
解決辦法
我們將Timer代碼加入到UI Runloop模式
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
NSTimer *timer = [NSTimer timerWithTimeInterval:1.0 target:self selector:@selector(updateTimer) userInfo:nil repeats:YES];
// 加入到UI模式
[[NSRunLoop currentRunLoop] addTimer:timer forMode:(NSRunLoopCommonModes)];
// NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(updateTimer) userInfo:nil repeats:YES];
}
- (void)updateTimer{
NSLog(@"%@",[NSThread currentThread]);
}
再次運行 發現TextView拖動的時候不會影響到Timer的運行了
第二個問題
雖然這么做不會影響到Timer但是如果我們在Timer刷新事件中加入耗時操作會影響到UI的操作。
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
NSTimer *timer = [NSTimer timerWithTimeInterval:1.0 target:self selector:@selector(updateTimer) userInfo:nil repeats:YES];
// 加入到UI模式
[[NSRunLoop currentRunLoop] addTimer:timer forMode:(NSRunLoopCommonModes)];
// NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(updateTimer) userInfo:nil repeats:YES];
}
- (void)updateTimer{
// 加入耗時操作
[NSThread sleepForTimeInterval:1.0];
NSLog(@"%@",[NSThread currentThread]);
}
再次運行 滑動UI發現 非常卡頓 ,這是因為Timer的事件影響了UI的正常運行。
解決辦法
耗時的方法不能放在主線程中來做,把耗時的操作放到子線程中。問題:我們把耗時的操作放在子線程中是不會卡頓UI了,但是如果我們更新UI比較耗時怎么辦呢?這個時候就要優化了
TableView加載超大的圖片
先說一個小問題,線程可以隨便開嗎?
答案是線程盡量不要開太多,太多的話,手機會發燙,也會更加耗電。所以盡可能的少開線程。
TableView加載大圖多Cell的時候,會造成哪些問題?
內存占用過大,這個我們在加載的時候每次清理子View解決
頁面會卡頓,是因為繪制UI的耗時操作
怎么做?
1: 繪制圖片的時候,實際上是走的Runloop,一次Runloop要繪制所有的耗時操作,那么我們可以把繪制UI的操作放在多個Runloop下而不是一次就繪制好它。
2:做法:在Runloop走一次的時候 ,我們把它拿出來,告訴它一次少做點UI操作就好了
先看一個加載大圖的Demo
發現非常卡頓
思路:
1:監聽Runloop的循環!用到C語言的框架,循環一次,調用一個回調函數
2:在回調函數里面進行加載圖片的事情(執行一次任務)TableView最清楚
3:提供一個添加任務的方法,加載圖片的方法 CellForRow
4:在Runloop回調函數中取出來一個個取出來執行。
- Runloop的回調事件
/* Run Loop Observer Activities */
typedef CF_OPTIONS(CFOptionFlags, CFRunLoopActivity) {
kCFRunLoopEntry = (1UL << 0), // 進入
kCFRunLoopBeforeTimers = (1UL << 1), // 即將進入Timer
kCFRunLoopBeforeSources = (1UL << 2), //
kCFRunLoopBeforeWaiting = (1UL << 5), // 即將進行休眠
kCFRunLoopAfterWaiting = (1UL << 6),
kCFRunLoopExit = (1UL << 7), // 即將退出
kCFRunLoopAllActivities = 0x0FFFFFFFU
};
- Runloop的監聽事件
每次狀態改變,將會回調這個監聽事件
C語言通過函數指針來進行回調 后綴Ref
typedef struct CF_BRIDGED_MUTABLE_TYPE(id) __CFRunLoopObserver * CFRunLoopObserverRef;
Runloop監聽的代碼
//
// ViewController.m
// 加載大圖的Demo
//
// Created by mac on 2017/2/18.
// Copyright ? 2017年 mac. All rights reserved.
//
#import "ViewController.h"
#import "MyCell.h"
/**
定義一個block
*/
typedef BOOL(^RunloopBlock)(void);
// 加載最后面的30張
@interface ViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (weak, nonatomic) IBOutlet UITableView *tableView;
/**
存放任務的數組
*/
@property (nonatomic, strong) NSMutableArray * tasks;
/**
任務標記
*/
@property (nonatomic, strong) NSMutableArray * tasksKeys;
/**
最大任務數
*/
@property (nonatomic, assign) NSInteger max;
/**
Timer
*/
@property (nonatomic, strong) NSTimer * timer;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
_max = 30;
_tasks = [NSMutableArray array];
_tasksKeys = [NSMutableArray array];
// Do any additional setup after loading the view, typically from a nib.
// _timer = [NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(timeFiredMethod) userInfo:nil repeats:YES];
// 注冊監聽
[self addRunloopObserver];
}
- (void)timeFiredMethod{
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return 399;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
MyCell *cell = [tableView dequeueReusableCellWithIdentifier:@"myCell"];
NSString *path1 = [[NSBundle mainBundle] pathForResource:@"spaceship" ofType:@"png"];
UIImage *image2 = [UIImage imageWithContentsOfFile:path1];
// imageView2.contentMode = UIViewContentModeScaleAspectFit;
// imageView2.image = image2;
// 卡頓原因 當我們拖拽Cell的時候 Runloop一次循環就要繪制完所有的UI刷新工作
// 不要在一次Runloop的時候繪制所有的圖片,我們可以在每次循環的時候只繪制一張圖片
// 所以 在這里不要直接加載圖片 將加載圖片的代碼丟給Runloop 用什么東西放代碼? Block!
// 添加到我的任務里
[self addTask:^BOOL{
[UIView transitionWithView:cell.contentView duration:0.3 options:(UIViewAnimationOptionCurveEaseInOut) animations:^{
cell.imageViewOne.image = image2;
} completion:^(BOOL finished) {
}];
return YES;
} withKey:indexPath];
[self addTask:^BOOL{
[UIView transitionWithView:cell.contentView duration:0.3 options:(UIViewAnimationOptionCurveEaseInOut) animations:^{
cell.imageViewTwo.image = image2;
} completion:^(BOOL finished) {
}];
return YES;
} withKey:indexPath];
[self addTask:^BOOL{
[UIView transitionWithView:cell.contentView duration:0.3 options:(UIViewAnimationOptionCurveEaseInOut) animations:^{
cell.imageViewThree.image = image2;
} completion:^(BOOL finished) {
}];
return YES;
} withKey:indexPath];
return cell;
}
#pragma mark -- <Runloop>
// MARK: 添加任務
- (void)addTask:(RunloopBlock)unit withKey:(id)key{
// 添加任務
[self.tasks addObject:unit];
// 添加任務的Key
[self.tasksKeys addObject:key];
// 保證之前沒有顯示出來的任務 不再浪費時間加載 保證每次只執行最后添加的30個任務
if (self.tasks.count > self.max) {
// 移除任務
[self.tasks removeObjectAtIndex:0];
// 移除任務的key
[self.tasksKeys removeObjectAtIndex:0];
}
}
#pragma mark -- 回調函數
// 定義回調函數 一次Runloop來一次
static void Callback(CFRunLoopObserverRef observer, CFRunLoopActivity activity, void *info){
NSLog(@"%s", __func__);
NSLog(@"%@",info); // info 代表的就是當前的控制器
// 通過橋接拿到VC
ViewController *vc = (__bridge ViewController *)(info);
// 任務完畢 返回
if (vc.tasks.count == 0) {
return;
}
BOOL result = NO;
// 這里用死循環 來循環取出并執行任務
while (result == NO && vc.tasks.count) {
// 即判斷Block不為空 又判斷task不為空
RunloopBlock unit = vc.tasks.firstObject;
// 執行任務
result = unit();
// 干掉第一個任務
[vc.tasks removeObjectAtIndex:0];
// 干掉標識
[vc.tasksKeys removeObjectAtIndex:0];
}
}
/**
這里面都是C語言 -- 添加一個監聽者
*/
- (void)addRunloopObserver{
// 獲取當前的Runloop
CFRunLoopRef runloop = CFRunLoopGetCurrent();
// 定義一個context
CFRunLoopObserverContext context = {
0,
(__bridge void *)(self),
&CFRetain,
&CFRelease,
NULL
};
// 定義一個觀察者
static CFRunLoopObserverRef defaultModeObserver;
// 創建我們的觀察者
defaultModeObserver = CFRunLoopObserverCreate(NULL,
kCFRunLoopBeforeWaiting,
YES,
NSIntegerMax - 999,
&Callback,
&context
);
// 添加當前Runloop的觀察者
CFRunLoopAddObserver(runloop, defaultModeObserver, kCFRunLoopDefaultMode);
// C語言有Create 就需要release
CFRelease(defaultModeObserver);
}
@end