美文网首页
科学的撸一个秒杀定时器

科学的撸一个秒杀定时器

作者: 花早 | 来源:发表于2016-09-15 21:48 被阅读217次

    虽然TableView的重用机制很好用,但是....总有一些很尴尬的业务场景…比如定时器的复用…假如在Cell中定义一个定时器简直重用起来就是爆炸...写之前我也翻阅了一些简书相关的文章,但是...实现起来都太过繁琐和复杂了,有使用通知的(这黑科技)还有用数组把秒数装起来的(坑爹???)….简直不堪入目...所以我决定自己动手丰衣足食,下面开搞吧

    思路现实##

    _面向对象开发,既然每一个秒杀的数据都是一个对象,只要我让每一个对象都管理自己的定时器不就行了?何必又通知又数组的....同时重用机制每次都是按照indexPath来取自身的Model更加不用纠结定时器重用的问题..好吧,应该这样写,问题都能迎刃而解吧??....先试试再说.. _

    • 首先是通过网络数据转化为Model...

    '' //  SeckillListModel.h
    '' //
    '' //  Created by HuaZao on 16/9/13.
    '' //  Copyright © 2016年 HuaZaoGYJ. All rights reserved.
    '' //
    '' 
    '' #import <Foundation/Foundation.h>
    '' 
    '' typedef NS_OPTIONS(NSUInteger,SeckillState) {
    ''     //未开始
    ''     SeckillUnStart       = 0,
    ''     //进行中
    ''     SeckillStarting,
    ''     //已经结束
    ''     SeckillOver,
    '' };
    '' 
    '' @interface SeckillListModel : NSObject
    '' 
    '' @property (strong,nonatomic) dispatch_source_t timer;
    '' 
    '' /** 图片 */
    '' @property (nonatomic, copy) NSString* logoImageList;
    '' 
    '' /** 产品唯一ID */
    '' @property (nonatomic, copy) NSString* product_mark_id;
    '' 
    '' /** 商品ID */
    '' @property (nonatomic, assign) NSInteger  Id;
    '' 
    '' /** 秒杀时间 */
    '' @property (nonatomic, copy) NSString* seckill_time;
    '' 
    '' /** 秒杀保持时间 */
    '' @property (nonatomic, assign) NSInteger  keep_time;
    '' 
    '' /** 已经等待了多长时间 */
    '' @property (nonatomic, assign) NSInteger waitTime;
    '' 
    '' /** 秒杀已经过去的时间 */
    '' @property (nonatomic, assign) NSInteger  pass_time;
    '' 
    '' /** 服务器离开始秒杀或者结束秒杀的时间差 */
    '' @property (nonatomic, assign) NSInteger  diffTime;
    '' 
    '' /** 名字 */
    '' @property (nonatomic, copy) NSString* name;
    '' 
    '' /** 状态 */
    '' @property (nonatomic, assign) SeckillState  state;
    '' 
    '' /** 价钱 */
    '' @property (nonatomic, assign) CGFloat  price;
    '' 
    '' @end
    ''
    

    Model中的** timer**字段就是负责这个秒杀产品的定时器,这个很重要整个实现的核心!

    • 接下来就是我们撸一个Cell出来

    ''//
    '' //  seckillingCollectionViewCell.m
    '' //
    '' //  Created by HuaZao on 16/9/12.
    '' //  Copyright © 2016年 HuaZaoGYJ. All rights reserved.
    '' //
    '' 
    '' #import "seckillingCollectionViewCell.h"
    '' 
    '' @interface seckillingCollectionViewCell()
    '' 
    '' 
    '' @end
    '' 
    '' @implementation seckillingCollectionViewCell
    '' 
    '' 
    '' -(void)loadCellDataWithProductInfoModel:(SeckillListModel *)model{
    ''     self.goodStr.text = model.name;
    ''     [self.goodImageView sd_setImageWithURL:[NSURL URLWithString:model.logoImageList] placeholderImage:[UIImage imageNamed:@"ty_tpjzsb_img_nor"]];
    ''     switch (model.state) {
    ''         case SeckillUnStart:
    ''             self.seckillWaitView.hidden = NO;
    ''             self.seckillOverView.hidden = YES;
    ''             self.seckillStartView.hidden = YES;
    ''             self.seckillWaitTime.text = model.seckill_time;
    ''             [self startSeckillWithModel:model];
    ''             break;
    ''         case SeckillStarting:
    ''             self.seckillWaitView.hidden = YES;
    ''             self.seckillOverView.hidden = YES;
    ''             self.seckillStartView.hidden = NO;
    ''             //开始倒计时
    ''             [self starTimeCountWithModel:model];
    ''             break;
    ''         case SeckillOver:
    ''             self.seckillWaitView.hidden = YES;
    ''             self.seckillOverView.hidden = NO;
    ''             self.seckillStartView.hidden = YES;
    ''             break;
    ''         default:
    ''             break;
    ''     }
    '' }
    '' 
    '' 
    '' /*开始秒杀*/
    '' -(void)startSeckillWithModel:(SeckillListModel *)model{
    ''     //应该去秒杀了
    ''     if (model.diffTime == 0) {
    ''         [self starTimeCountWithModel:model];
    ''         return;
    ''     }
    ''     
    ''     //取真正的时间差
    ''     __block NSInteger countDiffTime =  model.diffTime - model.pass_time;
    ''     
    ''     
    ''     if (model.timer == nil){
    ''         dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
    ''         model.timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0,queue);
    ''         dispatch_source_set_timer(model.timer,dispatch_walltime(NULL, 0),1.0*NSEC_PER_SEC, 0); //每秒执行
    ''         //启动定时器
    ''         dispatch_resume(model.timer);
    ''     }
    ''     
    ''     dispatch_source_set_event_handler(model.timer, ^{
    ''         countDiffTime --;
    ''         NSLog(@"商品秒杀倒计时-----%d---%@--",countDiffTime,model.timer);
    ''         if (countDiffTime == 0) {
    ''             //关闭这个定时器
    ''             dispatch_source_cancel(model.timer);
    ''             model.timer = nil;
    ''             model.pass_time = 0;
    ''             model.diffTime = 0;
    ''             model.state = SeckillStarting;
    ''             [self starTimeCountWithModel:model];
    ''             dispatch_async(dispatch_get_main_queue(), ^{
    ''                 self.seckillWaitView.hidden = YES;
    ''                 self.seckillOverView.hidden = YES;
    ''                 self.seckillStartView.hidden = NO;
    ''             });
    ''         }
    ''     });
    ''     
    '' }
    '' 
    '' 
    '' /*已经在秒杀了*/
    '' -(void)starTimeCountWithModel:(SeckillListModel *)model{
    ''     
    ''     //已经秒完了
    ''     if (model.pass_time >= model.keep_time || model.diffTime == model.keep_time){
    ''         self.seckillWaitView.hidden = YES;
    ''         self.seckillOverView.hidden = NO;
    ''         self.seckillStartView.hidden = YES;
    ''         return;
    ''     }
    ''     
    ''     //取真正的时间差
    ''     NSInteger deffTime = model.keep_time - model.diffTime - model.pass_time;
    ''     
    ''     //转分钟
    ''    __block NSInteger seckillHour = (deffTime/3600);
    ''    __block NSInteger seckillMinute = (deffTime/60) % 60;
    ''    __block NSInteger seckillSecond = deffTime % 60;
    ''     
    ''     if (model.timer == nil) {
    ''         dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
    ''         model.timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0,queue);
    ''         dispatch_source_set_timer(model.timer,dispatch_walltime(NULL, 0),1.0*NSEC_PER_SEC, 0); //每秒执行
    ''         //启动定时器
    ''         dispatch_resume(model.timer);
    ''     }
    ''     
    ''     dispatch_source_set_event_handler(model.timer, ^{
    ''             seckillSecond --;
    ''         if (seckillSecond == 0) {
    ''             if (seckillMinute == 0) {
    ''                 seckillMinute = 0;
    ''                 
    ''             }else{
    ''                 seckillMinute --;
    ''                 seckillSecond = 60;
    ''             }
    ''         }
    ''         
    ''         if (seckillMinute == 0) {
    ''             if (seckillHour == 0) {
    ''                 seckillHour = 0;
    ''             }else{
    ''                 seckillHour --;
    ''                 seckillMinute = 60;
    ''             }
    ''         }
    '' 
    ''         NSLog(@"-----%d---%@--",model.pass_time,model.timer);
    ''         if (seckillHour == 0 && seckillMinute ==0 && seckillSecond == 0) {
    ''             //关闭这个定时器
    ''             dispatch_source_cancel(model.timer);
    ''             model.timer = nil;
    ''             model.pass_time = 0;
    ''             dispatch_async(dispatch_get_main_queue(), ^{
    ''                 self.seckillWaitView.hidden = YES;
    ''                 self.seckillOverView.hidden = NO;
    ''                 self.seckillStartView.hidden = YES;
    ''             });
    ''         }
    ''         dispatch_async(dispatch_get_main_queue(), ^{
    ''             self.seckillTime.text = [NSString stringWithFormat:@"%02d:%02d:%02d",(int)seckillHour,(int)seckillMinute,(int)seckillSecond];
    ''         });
    ''     });
    '' 
    '' }
    '' '' @end
    '' 
    '' 
    **上面的代码比较多,其实核心代码就几句**
    ''   if (model.timer == nil){
    '' ''         dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
    '' ''         model.timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0,queue);
    '' ''         dispatch_source_set_timer(model.timer,dispatch_walltime(NULL, 0),1.0*NSEC_PER_SEC, 0); 
    '' ''         dispatch_resume(model.timer);
    '' ''     }
    '' ''     dispatch_source_set_event_handler(model.timer, ^{
    '' //做你要处理的事情
    '' });
    

    我们只保证每一个Model的定时器只存在一个Cell重用的时候我们就照样拿出来执行dispatch_source_set_event_handler这个函数就没问题了..这样我们上面的需求已经差不多实现了..不过总觉得差了一点什么.....总有点不对劲....如果这个Model定时器没初始化到岂不是坑爹????这样会造成巨大误差....下面把这个问题解决了吧...那我只需要…在拿到数据的时候计时已经过去的时间不就可以了吗???

    • 解决Cell没有初始化定时器造成的问题

    在控制器或者View中定义一个定时器,用来计时已经过去了多少时间

    ''@implementation seckillingView
    '' 
    '' /*开始倒计时多余的时间*/
    '' -(void)countPassTime{
    ''     dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
    ''     _passTime = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0,queue);
    ''     dispatch_source_set_timer(_passTime,dispatch_walltime(NULL, 0),1.0*NSEC_PER_SEC, 0); //每秒执行
    ''     dispatch_source_set_event_handler(_passTime, ^{
    ''         for (SeckillListModel *model in self.viewModel.seckillDataSource) {
    ''             model.pass_time ++;
    ''         }
    ''     });
    ''     //启动定时器
    ''     dispatch_resume(_passTime);
    '' }
    '' @end
    

    在Model中定义一个PassTime用来计算过去的多少时间,具体逻辑可以看前面Cell的实现,我们只需要把服务器时间减去passtime这样就可以解决刚刚的问题了,运行了一下基本实现需求了....收工0.0

    • View源码

    ''//
    '' //  seckillingView.m
    '' //
    '' //  Created by HuaZao on 16/9/12.
    '' //  Copyright © 2016年 HuaZaoGYJ. All rights reserved.
    '' //
    '' #import "seckillingCollectionViewCell.h"
    '' #import "seckillingView.h"
    '' #import "TTDKillGoodInfoViewController.h"
    '' #import "indexViewController.h"
    '' @interface  seckillingView()<UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout>
    '' 
    '' @property (strong,nonatomic) dispatch_source_t passTime;
    '' 
    '' @end
    '' 
    '' @implementation seckillingView
    '' 
    '' /*开始倒计时多余的时间*/
    '' -(void)countPassTime{
    ''     dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
    ''     _passTime = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0,queue);
    ''     dispatch_source_set_timer(_passTime,dispatch_walltime(NULL, 0),1.0*NSEC_PER_SEC, 0); //每秒执行
    ''     dispatch_source_set_event_handler(_passTime, ^{
    ''         for (SeckillListModel *model in self.viewModel.seckillDataSource) {
    ''             model.pass_time ++;
    ''         }
    ''     });
    ''     //启动定时器
    ''     dispatch_resume(_passTime);
    '' }
    '' 
    '' -(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    ''     return self.viewModel.seckillDataSource.count;
    '' }
    '' 
    '' -(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    ''     seckillingCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"seckillCell" forIndexPath:indexPath];
    ''     [cell loadCellDataWithProductInfoModel:self.viewModel.seckillDataSource[indexPath.row]];
    ''     return cell;
    '' }
    '' 
    '' 
    '' -(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    ''     TTDKillGoodInfoViewController *killVc = [[TTDKillGoodInfoViewController alloc] init];
    ''     killVc.seckModel = self.viewModel.seckillDataSource[indexPath.row];
    ''     killVc.goodId = self.viewModel.seckillDataSource[indexPath.row].product_mark_id;
    ''     [self.viewModel.indexVc.navigationController pushViewController:killVc animated:YES];
    '' }
    '' 
    '' 
    '' @end
    

    相关文章

      网友评论

          本文标题:科学的撸一个秒杀定时器

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