UITableView优化--使用runloop,优化耗时较多的事件

处理逻辑:

runloop运行循环,每次循环的时候都会处理多个cell的显示,现在需要每次循环只处理一个cell的显示

头文件.h

//
//  HMCRunLoopTask.h
//  test
//
//  Created by 胡梦驰 on 2018/7/13.
//  Copyright © 2018年 胡梦驰. All rights reserved.
//

#import 

typedef void(^RunloopBlock) (void);

@interface HMCRunLoopTask : NSObject

//添加任务
- (void)addTask:(RunloopBlock)task;

//开始定时器
- (void)startTimer;

//结束定时器
- (void)endTimer;

@end

实现文件.m

//
//  HMCRunLoopTask.m
//  test
//
//  Created by 胡梦驰 on 2018/7/13.
//  Copyright © 2018年 胡梦驰. All rights reserved.
//

#import "HMCRunLoopTask.h"

#define MaxTaskCount 10
@interface HMCRunLoopTask()

@property (nonatomic, strong) NSMutableArray *taskArray;
@property (nonatomic, strong) NSTimer *timer;

@end

@implementation HMCRunLoopTask

- (instancetype)init{
    if(self=[super init]){
        self.taskArray = [NSMutableArray array];
        [self addRunloopObverser];
    }
    return self;
}

- (void)timerMethod{
    //什么事情也不做!
    NSLog(@"runlooptask timer 还在repeat,没有杀掉");
}

//开始定时器
- (void)startTimer{
    if(self.timer){
        [self.timer invalidate];
        self.timer = nil;
    }
    self.timer = [NSTimer scheduledTimerWithTimeInterval:0.001 target:self selector:@selector(timerMethod) userInfo:nil repeats:YES];
}

//结束定时器
- (void)endTimer{
    [self.timer invalidate];
}

#pragma mark - <关于C语言Runloop>
//添加任务
- (void)addTask:(RunloopBlock)task{
    [self.taskArray addObject:task];
    if(self.taskArray.count > MaxTaskCount){
        [self.taskArray removeObjectAtIndex:0];
    }
}
//回调方法
static void callBack(CFRunLoopObserverRef observer, CFRunLoopActivity activity, void *info){
    HMCRunLoopTask *vc = (__bridge HMCRunLoopTask*)info;
    if(vc.taskArray.count==0){
        return;
    }
    RunloopBlock task = vc.taskArray.firstObject;
    task();
    [vc.taskArray removeObjectAtIndex:0];
}

//添加Runloop
- (void)addRunloopObverser{
    //拿到当前的Runloop
    CFRunLoopRef runloop = CFRunLoopGetCurrent();
    CFRunLoopObserverContext context = {
        0,
        (__bridge void *)(self),
        &CFRetain,
        &CFRelease,
        NULL
    };
    
    //定义一个观察者,static内存中只存在一个
    static CFRunLoopObserverRef obverser;
    //创建一个观察者
    obverser = CFRunLoopObserverCreate(NULL, kCFRunLoopAfterWaiting, YES, 0, &callBack, &context);
    //添加观察者!!!
    CFRunLoopAddObserver(runloop, obverser, kCFRunLoopCommonModes);
    
    //release
    CFRelease(obverser);
}

@end


实现实例:


@interface ViewController ()

@property (nonatomic, strong) HMCRunLoopTask *runloopTask;

@end

@implementation SoldOutBidController

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self.runloopTask startTimer];
}

- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    [self.runloopTask endTimer];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.runloopTask = [[HMCRunLoopTask alloc] init];
    // Do any additional setup after loading the view from its nib.
}

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath{
    __weak typeof(self) weakSelf = self;
    //将耗时的事件添加到runloop中
    [self.runloopTask addTask:^{
        [cell updateDisplayModelShouKong:weakSelf.bidArray[indexPath.row]];
    }];
}


你可能感兴趣的:(iOS开发,tableview,优化,runloop)