Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

关于文件 断点 缓存下载这个案例的一些问题 #49

Open
OmgKevin opened this issue Sep 16, 2019 · 9 comments
Open

关于文件 断点 缓存下载这个案例的一些问题 #49

OmgKevin opened this issue Sep 16, 2019 · 9 comments

Comments

@OmgKevin
Copy link

参考的您这个案例中我想要实现相同的需求,所以集中参考了您的LMJOfflineDownloadViewController.m这个类文件中的实现方式,但是有一些东西看的不是很明白。
image

@OmgKevin
Copy link
Author

我这里是自定义cell
image
在SDFileDownloadView.m的- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath方法中实现:

  • (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
    {
    SDFileTableViewCell *cell = [_tableView cellForRowAtIndexPath:indexPath];

    [self cellDidSelectRow:cell];

}

  • (void) cellDidSelectRow :(SDFileTableViewCell *)cell {

    // 1.0先隐藏掉下载按钮
    [cell.selectButton setHidden:YES];
    // 1.1显示下载进度label
    [cell.downloadSpeedLabel setHidden:NO];

    // 2.1 下载文件url
    NSArray<NSString *> *urls = @[

      @"http://teaching.csse.uwa.edu.au/units/CITS4401/practicals/James1_files/SPMP1.pdf",
      @"http://down.51rc.com/dwndoc/WrittenExamination/WrittenExperiences/dwn00006795.doc",
      @"http://video1.remindchat.com/20190905/1gEji0Sv/mp4/1gEji0Sv.mp4",
      @"https://www.sample-videos.com/video123/mp4/720/big_buck_bunny_720p_20mb.mp4",
      @"http://clips.vorwaerts-gmbh.de/big_buck_bunny.mp4",
      @"http://mirror.aarnet.edu.au/pub/TED-talks/911Mothers_2010W-480p.mp4",
                                
     ];
    

    __weak typeof(self)weakSelf = self;

    [urls enumerateObjectsUsingBlock:^(NSString * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {

      MJDownloadInfo *info = [[MJDownloadManager defaultManager] downloadInfoForURL:obj];
      
      if (info.state == MJDownloadStateCompleted) {
          // 下载完成之后,下载中图标更换为下载完成图标
          [cell.downloadSpeedLabel setHidden:YES];
          [cell.selectButton setHidden:NO];
          [cell.selectButton setImage:[UIImage imageNamed:@"downloadok"] forState:UIControlStateNormal];
      }else {
          // 未下载状态
          NSLog(@"222222222222");
      }
      
      
      if (info.state == MJDownloadStateResumed || info.state == MJDownloadStateWillResume) {
          
          
      } else if (info.state == MJDownloadStateSuspened || info.state == MJDownloadStateNone) {
          
          [[MJDownloadManager defaultManager] download:obj progress:^(NSInteger bytesWritten, NSInteger totalBytesWritten, NSInteger totalBytesExpectedToWrite) {
              
              dispatch_async(dispatch_get_main_queue(), ^{
                  
                  // 2.2 下载中进度显示
                  cell.downloadSpeedLabel.text = [NSString stringWithFormat:@"%.2f%%", (CGFloat)totalBytesWritten / totalBytesExpectedToWrite * 100.0];
                  
              });
              
          } state:^(MJDownloadState state, NSString *file, NSError *error) {
              
              dispatch_async(dispatch_get_main_queue(), ^{
                  
                  // 2.3 下载完成,改变按钮状态,保存下载文件路径,保存cell点击状态
                  if (state == MJDownloadStateCompleted) {
                      
                      [cell.downloadSpeedLabel setHidden:YES];
                      [cell.selectButton setHidden:NO];
                      [cell.selectButton setImage:[UIImage imageNamed:@"downloadok"] forState:UIControlStateNormal];
                      
                      
                      NSLog(@"打印保存文件路径地址 :%@",file);
                      
                  }
              });
          }];
          
      }else if (info.state == MJDownloadStateCompleted) {
          
          // 已下载完成的文件取url链接跳转阅读控制器
          NSString * filepath = [NSString stringWithFormat:@"%@",info.file];
          
          if ([self.delegate respondsToSelector:@selector(cellFilePathStr:)]) {
              
              // 移除下载列表视图
              [self removeFromSuperview];
              // 代理方法跳转阅读文档控制器
              [self.delegate cellFilePathStr:filepath];
          }
      }
    

    }];
    }

发现点击一行cell时,可以正常实现下载文件,但是当我想实现多点击几行cell让它多文件同时下载的时候,不知道应该怎么去实现,希望得到您的帮助

@NJHu
Copy link
Owner

NJHu commented Sep 16, 2019

考虑到cell的复用问题; 建议您这里修改下, 优先更新模型数据, 再更新UI数据

@NJHu
Copy link
Owner

NJHu commented Sep 16, 2019

抱歉, 注释写的不是很清楚, 补了些注释, 看下还有什么疑问吗?

// 数据
NSArray<NSString *> *urls = @[
@"http://teaching.csse.uwa.edu.au/units/CITS4401/practicals/James1_files/SPMP1.pdf",
@"http://down.51rc.com/dwndoc/WrittenExamination/WrittenExperiences/dwn00006795.doc",
@"http://video1.remindchat.com/20190905/1gEji0Sv/mp4/1gEji0Sv.mp4",
@"https://www.sample-videos.com/video123/mp4/720/big_buck_bunny_720p_20mb.mp4",
@"http://clips.vorwaerts-gmbh.de/big_buck_bunny.mp4",
@"http://mirror.aarnet.edu.au/pub/TED-talks/911Mothers_2010W-480p.mp4",
];

self.title = @"点击Cell开始/暂停下载";
LMJWeak(self);
// 遍历URL个数创建对应的模型数组
[urls enumerateObjectsUsingBlock:^(NSString * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
    
    // 获取下载文件对象
    MJDownloadInfo *info = [[MJDownloadManager defaultManager] downloadInfoForURL:obj];
    
    NSString *subTitle = nil;
    // 比对缓存是否下载完毕
    if (info.state == MJDownloadStateCompleted) {
        subTitle = @"播放";
    }else {
        // 比对缓存进度
        CGFloat progress = ((CGFloat)info.totalBytesWritten) / info.totalBytesExpectedToWrite * 100;
        subTitle = [NSString stringWithFormat:@"进度: %.2f%%, 点击开始", isnan(progress) ? 0 : progress];
    }
    
    // 添加数据模型, 和  绑定点击事件;
    // 考虑到cell的复用问题, 这个cell点击的时候, 调用了模型的itemOperation回调,
    self.addItem([LMJWordItem itemWithTitle:[obj.lastPathComponent substringToIndex:5] subTitle:subTitle itemOperation:^(NSIndexPath *indexPath) {
        
        // 文件下载状态: 下载中和在下载队列排队, 最大3个下载
        if (info.state == MJDownloadStateResumed || info.state == MJDownloadStateWillResume) {
            
            // 暂停
            [[MJDownloadManager defaultManager] suspend:info.url];
            
            // 获取进度
            CGFloat progress = ((CGFloat)info.totalBytesWritten) / info.totalBytesExpectedToWrite * 100;
            
            // 刷新模型
            weakself.sections.firstObject.items[indexPath.row].subTitle = [NSString stringWithFormat:@"暂停中..进度: %.2f%%", isnan(progress) ? 0 : progress];
            
            // 刷新UI, 获取不到就不刷新UI, 下次滚动cell赋值模型的时候, 还会刷新模型数据
            ((LMJSettingCell *)[weakself.tableView cellForRowAtIndexPath:indexPath]).item = weakself.sections.firstObject.items[indexPath.row];
            
        } else if (info.state == MJDownloadStateSuspened || info.state == MJDownloadStateNone) { // 暂停中和无状态; 开始下载
            
            // 开始下载obj = Url
            [[MJDownloadManager defaultManager] download:obj progress:^(NSInteger bytesWritten, NSInteger totalBytesWritten, NSInteger totalBytesExpectedToWrite) {
                dispatch_async(dispatch_get_main_queue(), ^{
                    
                    // 更新模型
                    weakself.sections.firstObject.items[indexPath.row].subTitle = [NSString stringWithFormat:@"进度: %.2f%%", (CGFloat)totalBytesWritten / totalBytesExpectedToWrite * 100.0];

                    // 更新视图; 获取不到cell就不刷新UI, 下次滚动cell然后赋值模型的时候, 还会刷新模型数据
                    ((LMJSettingCell *)[weakself.tableView cellForRowAtIndexPath:indexPath]).item = weakself.sections.firstObject.items[indexPath.row];
                });
            } state:^(MJDownloadState state, NSString *file, NSError *error) {
                // 主线程刷新UI
                dispatch_async(dispatch_get_main_queue(), ^{
                    if (state == MJDownloadStateCompleted) {
                        // 更新模型
                        weakself.sections.firstObject.items[indexPath.row].subTitle = @"播放";
                        // 更新视图; 获取不到cell就不刷新UI, 下次滚动cell然后赋值模型的时候, 还会刷新模型数据
                        ((LMJSettingCell *)[weakself.tableView cellForRowAtIndexPath:indexPath]).item = weakself.sections.firstObject.items[indexPath.row];
                    }
                });
            }];
            
        }else if (info.state == MJDownloadStateCompleted) { // 文件是下载完毕的状态
            // 跳转播放, 根据实际情况点击
            VIDMoviePlayerViewController *playerVc = [[VIDMoviePlayerViewController alloc] init];
            playerVc.videoURL = [NSString stringWithFormat:@"file://%@", info.file];
            [weakself.navigationController pushViewController:playerVc animated:YES];
            
        }
        
    }]);
}];

// 添加2个操作模型, 绑定模型itemOperation操作
self.addItem([LMJWordItem itemWithTitle:@"全部开始" subTitle: nil itemOperation:^(NSIndexPath *indexPath) {
    [[MJDownloadManager defaultManager] resumeAll];
}]).addItem([LMJWordItem itemWithTitle:@"全部暂停" subTitle: nil itemOperation:^(NSIndexPath *indexPath) {
    [[MJDownloadManager defaultManager] suspendAll];
}]);

@NJHu
Copy link
Owner

NJHu commented Sep 16, 2019

尝试用了您这边的数据试了试, 应该是可以的; 建议您参考下cell的数据刷新和赋值模型数据的逻辑

image

image

image

@OmgKevin
Copy link
Author

OmgKevin commented Sep 16, 2019 via email

@OmgKevin
Copy link
Author

OmgKevin commented Sep 16, 2019 via email

@NJHu
Copy link
Owner

NJHu commented Sep 16, 2019

明天demo发下哦, 上传到您自己的github的仓库

@OmgKevin
Copy link
Author

OmgKevin commented Sep 16, 2019 via email

@OmgKevin
Copy link
Author

您好,我这边已经将自己的demo上传至GitHub,地址 :https://github.com/OmgKevin/FileDownload
,请帮忙看一下呀,万分感谢

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants