美文网首页
RunLoop 预处理缓存任务

RunLoop 预处理缓存任务

作者: iOS104 | 来源:发表于2017-05-05 18:04 被阅读100次

    将一个RunLoop的一些耗时任务分拆到多个RunLoop里面处理

    需求:cell 里面需要加载大图(3M),保证Cell滚动的时候,UITableView不卡顿

    卡顿原因:

    在一次runloop中绘制大量的图片,所以UI会卡顿

    解决方案:(一次runloop,只绘制一张图片)

    • 1、监听Runloop 循环,循环一次,调用一次回调函数
    • 2、回调函数里面做加载图片的事情(执行一次任务)
    • 3、提供一个添加任务的方法,不要直接加载图片,你将加载图片的代码都给RunLoop
    • 4、回调函数里面将任务一个一个取出来
    • 5、手动向 RunLoop 中添加 Source 任务(由应用发起和处理的是 Source 0 任务)。通过开启定时,NSTimeInterval = 0.001

    代码链接

    #import "ViewController.h"
    
    typedef BOOL(^RunloopBlock)(void);
    
    static NSString *IDENTIFIER = @"IDENTIFIER";
    static CGFloat CELL_HEIGHT = 135.f;
    
    @interface ViewController () <UITableViewDataSource, UITableViewDelegate>
    
    @property (nonatomic, strong) NSMutableArray *tasks;
    @property (nonatomic, strong) NSMutableArray *tasksKeys;
    @property (nonatomic, assign) NSUInteger max;
    @property (nonatomic, strong) NSTimer * timer;
    @property (nonatomic, strong) UITableView *exampleTableView;
    
    @end
    
    
    @implementation ViewController
    
    - (void)viewDidLoad {
        [super viewDidLoad];
        _max = 20;
        _tasks = [NSMutableArray array];
        _tasksKeys = [NSMutableArray array];
        
        self.exampleTableView = [[UITableView alloc] initWithFrame:self.view.bounds];
        self.exampleTableView.delegate = self;
        self.exampleTableView.dataSource = self;
        [self.exampleTableView registerClass:[UITableViewCell class] forCellReuseIdentifier:IDENTIFIER];
        [self.view addSubview:self.exampleTableView];
        
        _timer = [NSTimer scheduledTimerWithTimeInterval:0.001 target:self selector:@selector(_timerFiredMethod) userInfo:nil repeats:YES];
        
        [self addRunloopObserver];
    }
    
    - (void)_timerFiredMethod{
        
    }
    
    + (void)addImage1With:(UITableViewCell *)cell{
        
        UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(5, 20, 85, 85)];
        imageView.tag = 1;
        NSString *path1 = [[NSBundle mainBundle] pathForResource:@"spaceship" ofType:@"png"];
        UIImage *image = [UIImage imageWithContentsOfFile:path1];
        imageView.contentMode = UIViewContentModeScaleAspectFit;
        imageView.image = image;
        [UIView transitionWithView:cell.contentView duration:0.3 options:(UIViewAnimationOptionCurveEaseInOut | UIViewAnimationOptionTransitionCrossDissolve) animations:^{
            [cell.contentView addSubview:imageView];
        } completion:nil];
    }
    
    + (void)addImage2With:(UITableViewCell *)cell{
        
        UIImageView *imageView1 = [[UIImageView alloc] initWithFrame:CGRectMake(105, 20, 85, 85)];
        imageView1.tag = 2;
        NSString *path1 = [[NSBundle mainBundle] pathForResource:@"spaceship" ofType:@"png"];
        UIImage *image1 = [UIImage imageWithContentsOfFile:path1];
        imageView1.contentMode = UIViewContentModeScaleAspectFit;
        imageView1.image = image1;
        [UIView transitionWithView:cell.contentView duration:0.3 options:(UIViewAnimationOptionCurveEaseInOut | UIViewAnimationOptionTransitionCrossDissolve) animations:^{
            [cell.contentView addSubview:imageView1];
        } completion:nil];
    }
    
    + (void)addImage3With:(UITableViewCell *)cell{
        UIImageView *imageView2 = [[UIImageView alloc] initWithFrame:CGRectMake(200, 20, 85, 85)];
        imageView2.tag = 3;
        NSString *path1 = [[NSBundle mainBundle] pathForResource:@"spaceship" ofType:@"png"];
        UIImage *image2 = [UIImage imageWithContentsOfFile:path1];
        imageView2.contentMode = UIViewContentModeScaleAspectFit;
        imageView2.image = image2;
        [UIView transitionWithView:cell.contentView duration:0.3 options:(UIViewAnimationOptionCurveEaseInOut | UIViewAnimationOptionTransitionCrossDissolve) animations:^{
            [cell.contentView addSubview:imageView2];
        } completion:nil];
    }
    
    #pragma mark - UITableViewDelegate
    
    - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
        return CELL_HEIGHT;
    }
    
    #pragma mark - UITableViewDataSource
    
    - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
        return 399;
    }
    
    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:IDENTIFIER];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        
        for (NSInteger i = 1; i <= 3; i++) {
            
            [[cell.contentView viewWithTag:i] removeFromSuperview];
        }
        //不要直接加载图片!! 你将加载图片的代码!都给RunLoop!!
        [self addTask:^BOOL{
            [ViewController addImage1With:cell];
            return YES;
        } withKey:indexPath];
        [self addTask:^BOOL{
            [ViewController addImage2With:cell];
            return YES;
        } withKey:indexPath];
        [self addTask:^BOOL{
            [ViewController addImage3With:cell];
            return YES;
        } withKey:indexPath];
        
        return cell;
    }
    
    
    #pragma mark - <RunLoop>
    
    - (void)addTask:(RunloopBlock)unit withKey:(id)key{
        [self.tasks addObject:unit];
        [self.tasksKeys addObject:key];
        
        if (self.tasks.count > self.max) {
            [self.tasks removeObjectAtIndex:0];
            [self.tasksKeys removeObjectAtIndex:0];
        }
        
    }
    
    //定义一个回调函数  一次RunLoop来一次
    static void Callback(CFRunLoopObserverRef observer, CFRunLoopActivity activity, void *info){
        ViewController * vc = (__bridge ViewController *)(info);
        if (vc.tasks.count == 0) {
            return;
        }
        BOOL result = NO;
        while (result == NO && vc.tasks.count) {
            RunloopBlock unit = vc.tasks.firstObject;
            //执行任务
            result = unit();
            [vc.tasks removeObjectAtIndex:0];
            [vc.tasksKeys removeObjectAtIndex:0];
        }
        
    }
    
    // 添加一个监听者
    - (void)addRunloopObserver{
        CFRunLoopRef runloop = CFRunLoopGetCurrent();
        CFRunLoopObserverContext context = {
            0,
            ( __bridge void *)(self),
            &CFRetain,
            &CFRelease,
            NULL
        };
        static CFRunLoopObserverRef defaultModeObsever;
        defaultModeObsever = CFRunLoopObserverCreate(NULL,
                                                     kCFRunLoopBeforeWaiting,
                                                     YES,
                                                     NSIntegerMax - 999,
                                                     &Callback,
                                                     &context
                                                     );
        
        //添加当前RunLoop的观察者
        CFRunLoopAddObserver(runloop, defaultModeObsever, kCFRunLoopDefaultMode);
        CFRelease(defaultModeObsever);
        
    }
    
    

    另一种解决方案:

    利用RunLoop空闲时间执行预缓存任务(孙源大神的UITableView-FDTemplateLayoutCell

    需求:

    利用RunLoop空闲时间执行预缓存任务(cell高度的计算)

    解决方案:

    • 1、分解成多个RunLoop Source任务
    • 2、RunLoop 处于休眠状态,则唤醒它处理事件
    • 3、每个 RunLoopObserver 回调时都把第一个任务拿出来分发

    假设列表有 20 个 cell,加载后展示了前 5 个,那么开启估算后 table view 只计算了这 5 个的高度,此时剩下 15 个就是“预缓存”的任务,而我们并不希望这 15 个计算任务在同一个 RunLoop 迭代中同步执行,这样会卡顿 UI,所以应该把它们分别分解到 15 个 RunLoop 迭代中执行,这时就需要手动向 RunLoop 中添加 Source 任务(由应用发起和处理的是 Source 0 任务)

    NSMutableArray *mutableIndexPathsToBePrecached = self.fd_allIndexPathsToBePrecached.mutableCopy;
    CFRunLoopObserverRef observer = CFRunLoopObserverCreateWithHandler(kCFAllocatorDefault, kCFRunLoopBeforeWaiting, true, 0, ^(CFRunLoopObserverRef observer, CFRunLoopActivity _) {
        if (mutableIndexPathsToBePrecached.count == 0) {
            CFRunLoopRemoveObserver(runLoop, observer, runLoopMode);
            CFRelease(observer); // 注意释放,否则会造成内存泄露
            return;
        }
        NSIndexPath *indexPath = mutableIndexPathsToBePrecached.firstObject;
        [mutableIndexPathsToBePrecached removeObject:indexPath];
        [self performSelector:@selector(fd_precacheIndexPathIfNeeded:)
                     onThread:[NSThread mainThread]
                   withObject:indexPath
                waitUntilDone:NO
                        modes:@[NSDefaultRunLoopMode]];
    });
    

    每个任务都被分配到下个“空闲” RunLoop 迭代中执行,其间但凡有滑动事件开始,Mode 切换成 UITrackingRunLoopMode,所有的“预缓存”任务的分发和执行都会自动暂定,最大程度保证滑动流畅。

    相关文章

      网友评论

          本文标题:RunLoop 预处理缓存任务

          本文链接:https://www.haomeiwen.com/subject/xzsstxtx.html