23.巧用runloop解决tableview频繁刷新问题

1.简述

在IM通讯中或者直播中,消息列表界面需要经常进行刷新, 如果每次接收到数据都刷新一次,有时在短时间内信息量太大的话,可能会造成刷新的过于频繁,为了解决这个问题,可以尝试下面的方法.

2.思路

可以通过控制器监听runloop的运行状态, 当runloop空闲的时候, 再进行UI的刷新. 并在刷新之前,设置好时间间隔,以及每次刷新的个数, 从而提升性能.

3.实现方案

3.1 一次性刷新方案(IM)
@interface ChatVC ()

// runloop观察者
@property (nonatomic, assign) CFRunLoopObserverRef observerRef;
// 是否需要刷新列表
@property (nonatomic, assign) BOOL isNeedToRefreshChatList;

@end


- (void)viewDidLoad {
    [self runloopObserver];
    [NSNotificationCenter.defaultCenter addObserver:self selector:@selector(needToRefreshChatList) name:kRefreshChatListNoti object:nil];
}


- (void)dealloc {

    [NSNotificationCenter.defaultCenter removeObserver:self];
    // 移除runloop的监听
    CFRunLoopRemoveObserver(CFRunLoopGetCurrent(), self.observerRef, kCFRunLoopCommonModes);
    NSLog(@"ChatVC dealloc");
}

// 刷新列表的通知方法
- (void)needToRefreshChatList {

    // 标记列表需要刷新
    self.isNeedToRefreshChatList = YES;
    // 唤醒runloop
    CFRunLoopWakeUp(CFRunLoopGetMain());
}


// 创建runloop监听
- (void)runloopObserver{

    @weakify(self)
    __block NSTimeInterval timeInterVal = [[NSDate date] timeIntervalSince1970];
    // 获取到runloop的观察者
    self.observerRef = CFRunLoopObserverCreateWithHandler(CFAllocatorGetDefault(), kCFRunLoopAllActivities, YES, 0, ^(CFRunLoopObserverRef observer, CFRunLoopActivity activity) {
        if (activity == kCFRunLoopBeforeWaiting) { // runloop空闲的时候刷新需要处理的列表
            
            // 控制刷新频率
            NSTimeInterval currentTimeInterval = [[NSDate date] timeIntervalSince1970];
            if (currentTimeInterval - timeInterVal < 0.1) {
                return;
            }
        
            timeInterVal = currentTimeInterval;
            
            if (weak_self.isNeedToRefreshChatList && weak_self.tableView) { // 判断是否需要刷新
                // 处理列表的刷新
                [weak_self refreshChatList];
                // 标记刷新完毕,无需重复刷新
                weak_self.isNeedToRefreshChatList = NO;
            }
        }
    });

    // 添加监听, 模式为 kCFRunLoopCommonModes
    CFRunLoopAddObserver(CFRunLoopGetCurrent(), self.observerRef, kCFRunLoopCommonModes);
}
3.2每次最多刷新10条数据,直到没有新的数据, 不在刷新(直播)
// 思路: 需要一个全局数组,存储所有的信息, 每次抽取其中的10条信息, 添加到tableview的数据源中, 并从数组中移除这10条数据, 刷新tableview进行展示

// 创建runloop 监听
- (void)runloopObserver{
    __weak typeof(self) weakSelf = self;
    __block NSTimeInterval timeInterVal = [[NSDate date] timeIntervalSince1970];
    observerRef = CFRunLoopObserverCreateWithHandler(CFAllocatorGetDefault(), kCFRunLoopAllActivities, YES, 0, ^(CFRunLoopObserverRef observer, CFRunLoopActivity activity) {
        NSLog(@"当前线程是: %@", [NSThread currentThread]);
        // 设置刷新频率 最少0.5秒刷新一次
        //来处理数据量 (直播刷留言)
        if (activity == kCFRunLoopBeforeWaiting) {
            
            // 控制频率
            NSTimeInterval currentTimeInterVal = [[NSDate date] timeIntervalSince1970];
            if (currentTimeInterVal - timeInterVal < 0.2) {
                return ;
            }
            timeInterVal = currentTimeInterVal;
            
            // 处理数据
            [weakSelf.dataLock lock];
            NSArray *subArr = nil;
            if (weakSelf.newsDataArr.count > 0) { // 一次最多拿10条数据
                // 频率可以通过newsDataArr的数据量来控制
                NSRange range;
                if (weakSelf.newsDataArr.count >= 10) {
                    range = NSMakeRange(0, 10);
                }else{
                    range = NSMakeRange(0, weakSelf.newsDataArr.count-1);
                }
                
                subArr = [weakSelf.newsDataArr subarrayWithRange:range];
                [weakSelf.newsDataArr removeObjectsInRange:range];
                
            }
            
            [weakSelf.dataArr addObjectsFromArray:subArr];
            [weakSelf.dataLock unlock];
            [weakSelf.tableView reloadData]; // 刷新界面
            [weakSelf.tableView layoutIfNeeded];
            
            [weakSelf.tableView setContentOffset:CGPointMake(0, weakSelf.tableView.contentSize.height - weakSelf.tableView.frame.size.height)];
        }
        
    });
    CFRunLoopAddObserver(CFRunLoopGetCurrent(), observerRef, kCFRunLoopCommonModes);
}

你可能感兴趣的:(23.巧用runloop解决tableview频繁刷新问题)