美文网首页
关于RunLoop Task模式实现列表优化

关于RunLoop Task模式实现列表优化

作者: 海笙樾 | 来源:发表于2021-06-17 10:15 被阅读0次

原理:

RunLoop在循环过程中监听着port事件和timer事件,
当前线程有任务时,唤醒当当线程去执行任务,
任务执行完成以后,使当前线程进入休眠状态。

代码:.h

//
//  LWQ_RunloopTableView.h
//  BathoathProject
//
//  Created by 葫芦娃 on 2021/6/17.
//

#import <UIKit/UIKit.h>

NS_ASSUME_NONNULL_BEGIN

typedef void(^LoadOtherBlock)(id arguments);

@interface LWQ_RunloopTableView : UIViewController
//task模式优化列表
@property(nonatomic, strong) NSMutableArray *taskArray;
@property(nonatomic, assign) NSInteger maxTaskNumber;
@property(nonatomic, strong) NSTimer *timer;
@property(nonatomic, strong) UITableView *tableView;
@property(nonatomic, copy)LoadOtherBlock mainBlock;
- (void)addTask;
@end

NS_ASSUME_NONNULL_END

.m

//
//  LWQ_RunloopTableView.m
//  BathoathProject
//
//  Created by 葫芦娃 on 2021/6/17.
//

#import "LWQ_RunloopTableView.h"

@interface LWQ_RunloopTableView ()<UITableViewDelegate,UITableViewDataSource>


@end

@implementation LWQ_RunloopTableView

- (void)viewDidLoad {
    [super viewDidLoad];
//最大任务数
    self.maxTaskNumber=10;
    self.taskArray=[NSMutableArray new];
//定时器不断循环task
    self.timer = [NSTimer scheduledTimerWithTimeInterval:0.01 repeats:YES block:^(NSTimer * _Nonnull timer) {
        NSLog(@"timer");
        void(^task)() = [self.taskArray firstObject];
        if (task) {
            task();
        }
        [self.taskArray removeObject:task];
    }];
}
//RuntLoop列表优化
- (UITableView *)tableView{
    if (!_tableView) {
        _tableView=[[UITableView alloc]initWithFrame:CGRectMake(0, 0, KscreenWidth, KscreenHeight) style:UITableViewStylePlain];
        _tableView.delegate=self;
        _tableView.dataSource=self;
        _tableView.backgroundColor=[UIColor whiteColor];
        _tableView.estimatedRowHeight=100;
    }
    return _tableView;
}
- (void)addTask{
    void(^task)(void) =^{
        self.mainBlock(@"");
    };
  //添加任务
    [self.taskArray addObject:task];
    //超过任务数则删除任务
    if (self.taskArray.count == self.maxTaskNumber) {
        [self.taskArray removeObjectAtIndex:0];
    }
}


@end

//使用
继承于LWQ_RunloopTableView创建controller

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

里调用[self addTask];
调用Block

self.mainBlock = ^(id  _Nonnull arguments) {
        //你自己的cell赋值等处理
    };
//例如
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    LWQ_TableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:@"LWQ_TableViewCell" forIndexPath:indexPath];
    if (!cell) {
        cell=[[LWQ_TableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"LWQ_TableViewCell"];
    }
    [self addTask];
    self.mainBlock = ^(id  _Nonnull arguments) {
//你自己的代码
        cell.mainImageView.image = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"name" ofType:@"gif"]];
    };
    return cell;
}

文件位置
https://gitee.com/1023678795/runloop.git

相关文章

  • 关于RunLoop Task模式实现列表优化

    原理: 代码:.h .m //使用继承于LWQ_RunloopTableView创建controller在 里调用...

  • Runloop优化列表滑动卡顿

    Runloop优化列表滑动卡顿

  • airflow 动态创建task

    airflow 动态创建task 通过http接口获取一个列表结果,遍历列表值,每条记录动态创建一个task 实现...

  • RunLoop的各种运行模式

    RunLoop运行模式相关 Core Foundation框架中关于RunLoop的5个类 CFRunLoopRe...

  • RunLoop

    一、RunLoop是事件接受与分发机制的一个实现。 二、RunLoop中的五种模式: 三、每个RunLoop中有多...

  • iOS RunLoop

    关于 runloop 面试中经常被问到: 讲讲 RunLoop,项目中有用到吗? RunLoop内部实现逻辑? R...

  • iOS知识点目录

    Swift特性OC特性UI多线程、Runloop、RuntimeOC底层内存管理、数据存储性能优化设计模式IM常用...

  • iOS-runloop相关

    本篇涵盖runloop解释、应用、利用runloop优化程序等. 1.iOS RunLoop漫谈2.RunLoop...

  • iOS底层(八)_RunLoop_实际使用

    关于RunLoop基本的介绍请查看第一篇文章以下主要介绍项目中RunLoop的实际使用 一、NSTimer优化 备...

  • iOS Developer 初级面试常见问题总结

    iOS 开发 RunLoop Cell 图片异步加载优化 iOS 函数式编程的实现 && 响应式编程概念 内存恶鬼...

网友评论

      本文标题:关于RunLoop Task模式实现列表优化

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