Runloop

  • 平时开发 用不到

  • 保证我们的程序不退出

  • 负责监听事件,比如说触摸、时钟、网络、更新我们的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

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容