美文网首页
使用Runloop来优化UITableView加载

使用Runloop来优化UITableView加载

作者: 三生之二 | 来源:发表于2019-06-18 17:50 被阅读0次

    1、原理
    在Runtime等待的时候执行要优化的代码,将任务拆分细化,每次Runloop循环尽量做最少的事情。
    2、例子
    利用UITableView加载很多大图(这类的图片都是大图,为了模仿加载耗时的场景),如下:

    图片.png
    按普通的方法直接在cellForRowAtIndexPath中加载大图,代码如下:
    [cell setCellWithImage2:[UIImage imageWithContentsOfFile:image2Path]];
        [cell setCellWithImage1:[UIImage imageWithContentsOfFile:image1Path]];
        [cell setCellWithImage3:[UIImage imageWithContentsOfFile:image3Path]];
    

    在这样的代码下,我们的滑动会出现卡顿,体验不佳。因为页面需要在一次循环中加载3高清大图,并渲染出来。如果我们每次都支持以一张图,而且在Runloop进入等待时处理,或许就可以解决滑动卡顿的问题。Now,show me your code。

    - (void)addTask:(void(^)(void))task {
        @synchronized (self.taskLock) {
            [self.tasks addObject:task];
            if (self.tasks.count > self.maxTaskLength) {
                [self.tasks removeObjectAtIndex:0];
            }
            if (self.timer == nil) {
                self.timer = [NSTimer scheduledTimerWithTimeInterval:0.001 repeats:YES block:^(NSTimer * _Nonnull timer) {
                    // 什么都不做,只是为了保持主线程的Runloop的defaultmodel一直有事可做,不让Runloop休息,直到task都完成
                }];
            }
        }
    }
    
    void excureTasks(CFRunLoopObserverRef observer, CFRunLoopActivity activity, void *info) {
        ViewController *vc = (__bridge ViewController *)info;
        if (vc.tasks.count == 0) {
            return;
        }
        @synchronized (vc.taskLock) {
            void(^task)(void) = [vc.tasks firstObject];
            task();
            [vc.tasks removeObject:task];
            if (vc.tasks.count == 0) {
                [vc.timer invalidate];
                vc.timer = nil;
            }
        }
    }
    
    - (void)resumeRunloopObserver {
        CFRunLoopRef runloop = CFRunLoopGetMain();
        CFRunLoopObserverContext context = {
            0,
            (__bridge void *)self,
            &CFRetain,
            &CFRelease,
            NULL
        };
        static CFRunLoopObserverRef observer;
        observer = CFRunLoopObserverCreate(NULL, kCFRunLoopAfterWaiting, YES, 0, &excureTasks, &context);
        CFRunLoopAddObserver(runloop, observer, kCFRunLoopCommonModes);
        CFRelease(observer);
    }
    

    我们将每一张图片的加载当作一个任务,放在tasks数组中进行管理,并设置tasks最大的任务数量(手机屏幕一次最多加载的cell数量*每个cell的大图数量),以此来保证tasks中的任务刚好够一个屏幕显示,且不会浪费资源。这里的timer是相当于监工的角色,只要监工一直运行,Runloop就不会休息,保证我们的tasks都完成。这样我们将上面加载图片的代码需改如下:

        [self addTask:^{
            [cell setCellWithImage1:[UIImage imageWithContentsOfFile:image1Path]];
        }];
        [self addTask:^{
            [cell setCellWithImage2:[UIImage imageWithContentsOfFile:image2Path]];
        }];
        [self addTask:^{
            [cell setCellWithImage3:[UIImage imageWithContentsOfFile:image3Path]];
        }];
    

    再次运行,我们会发现,现在页面加载的瀑布流出现了,图片一张一张刷刷的出来了,滑动流畅了。
    那么感谢Runloop(这家伙真的很苦逼,好不容易休息一下,还要被我们派监工监视,😂)。

    了解Runloop可以参考大神的文章:
    https://blog.ibireme.com/2015/05/18/runloop/

    相关文章

      网友评论

          本文标题:使用Runloop来优化UITableView加载

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