美文网首页iOSiOS Developer
【讨论】关于iOS列表中的倒计时方案

【讨论】关于iOS列表中的倒计时方案

作者: 码痞 | 来源:发表于2017-03-01 15:51 被阅读284次

    在工作中遇过需要往列表中注入定时器的问题,
    pass了几个方案,最后将定时器的数量调整到了一个。
    目前该方案上线半年没啥问题……
    看了ios dev板块的另一篇文章后决定把我的方案也放上来讨论一下,请各位老师不吝赐教!

    我的方案是用一个countDonwManager来控制一个列表里所有的倒计时模块刷新;

    通过 countDownArray 存储所有需要倒计时的Cell的index,
    例如 第三、五、六个Cell需要刷新的话,则

      countDownArray = @[@2,@4,@6];
    

    定时器每1秒触发一次,触发时,取数据Array(存放列表所有Model的Array)中对应index位置的Model,对model中的倒计时计数进行-1的操作。

    一次操作完成后,通过系统提供的方法刷新TableView的对应Cell
    - (void)reloadRowsAtIndexPaths:(NSArray<NSIndexPath *> *)indexPaths withRowAnimation:(UITableViewRowAnimation)animation NS_AVAILABLE_IOS(3_0);

    大致代码如下

    - (void)creatTimer{
    //创建计时器方法
        if (self.timer) { //安全控制,若已开启过,重新初始化
          dispatch_cancel(self.timer);
          self.timer = nil;
          self.timerQueue = nil;
        }
    
        _timerQueue = dispatch_get_global_queue(0, DISPATCH_QUEUE_PRIORITY_DEFAULT);
        _timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, _timerQueue);
        dispatch_source_set_timer(_timer, DISPATCH_TIME_NOW, 1 * NSEC_PER_SEC, 0 * NSEC_PER_SEC);
        dispatch_source_set_event_handler(_timer, ^{
        
            if (self.countDownArray.count == 0) {
                //安全控制,若没有需要倒计时的对象,不创建/开启计时器
                if (self.timer) {
                    dispatch_cancel(self.timer);
                    self.timer = nil;
                    self.timerQueue = nil;
                }
                return;
            }
        
            //一个indexSet对象,用来存放本次操作后倒计时为0的对象,最后将该indexSet中的对象从self.countDownArray中移除
            NSMutableIndexSet* countDownFinishedIndexSet = [[NSMutableIndexSet alloc] init];
        
            for (NSUInteger i = 0;i < self.countDownArray.count;i++) {
            
                NSDictionary* dic = self.countDownArray[i]; // 从倒计时存放数组中取出一个countDownDemo
            
                NSNumber* index = [dic valueForKey:@"index"];
                NSInteger countDownIndex = [index integerValue];
                //从数据存储的array里取倒计时, -1
                InfoModel* model = bondlistArray[countDownIndex];
                NSInteger djs = [model.djs integerValue];
                //等于0时进行页面刷新
                if (djs <= 0) {
                    //倒计时已结束,做对应的处理
                    // do something;
                    [countDownFinishedIndexSet addIndex:i];  
                    continue;
                }
                //如果倒计时数字大于0时,递减
                if (djs > 0) {
                
                    djs --;
                    model.djs = [NSString stringWithFormat:@"%ld",djs];
                }
              }
        
        
            [_countDownArray removeObjectsAtIndexes:countDownFinishedIndexSet];//将已经结束的Index从countDownArray中移除
    
            //对应cell进行刷新
            dispatch_async(dispatch_get_main_queue(), ^{
            
                NSMutableArray* reloadArray = [[NSMutableArray alloc] init];
                for (NSNumber* indexNum in _countDownArray) {
                
                    NSInteger countDownIndex = [index integerValue];
                    NSIndexPath* row = [NSIndexPath indexPathForRow:[index integerValue] inSection:0];
                    [reloadArray addObject:row];
                }
            
                //通知绑定的类进行刷新
                //do something
                //例如下面这一句通知绑定的TableView刷新对应的Cell
                //[self.targetTableView reloadRowsAtIndexPaths:reloadArray withRowAnimation:UITableViewRowAnimationNone];
            });
        
        });
        dispatch_resume(_timer); //启动计时器
      }
    

    经过反思提出了三个问题:
    1.是否可以把

      //取数据Array(存放列表所有Model的Array)中对应index位置的Model,对model中的倒计时计数进行-1的操作。
    

    这一步解耦合,例如通过runtime给Model添加固定的倒计时属性,解决每个列表中model的倒计时字段可能不一样的问题?

    2.刷新对应Cell的消耗是否可以优化,比如仅刷新Cell中倒计时模块的数据?

    相关文章

      网友评论

        本文标题:【讨论】关于iOS列表中的倒计时方案

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