ALDownloadManager 基于Alamofire封装的下载器

本文介绍了一个名为ALDownloadManager的下载管理器,它支持断点续传、单文件及多文件的同时下载和顺序下载等功能。通过具体的实现代码展示了如何进行下载操作,包括开始下载、取消下载和挂起下载等。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

ALDownloadManager包含了断点续传,多文件顺序下载,多文件同时下载

同时下载

顺序下载

外层调用: 单文件下载

ALDownloadManager.shared.download(url: self.testUrl)?.downloadProgress(nil).downloadResponse(nil)
复制代码

多文件同时下载

ALDownloadManager.shared.changeDownloadState()
复制代码

多文件顺序下载

 ALDownloadManager.shared.changeWaitState(completeClose: nil)
复制代码

具体实现: 下载方法(默认断点续传)

   func download() {
       if let resumeData = cancelledData {
           let destination = createDestination(destinationPath: destinationPath)
           downloadRequest = manager?.download(resumingWith: resumeData, to: destination).response(completionHandler: { [weak self] (defresponse) in
               self?.cancelledData = defresponse.resumeData
           }).downloadProgress(closure: { (progress) in
               self.progress = progress
           }).response(completionHandler: { (defaultResponse) in
               self.respons = defaultResponse
           })
       }else{
           let destination = createDestination(destinationPath: destinationPath)
           if let url = downloadurl {
               downloadRequest = manager?.download(url, method: .get, parameters: nil, encoding: JSONEncoding.default, headers: nil, to: destination).response(completionHandler: { [weak self] (defresponse) in
                   self?.cancelledData = defresponse.resumeData
               }).downloadProgress(closure: { (progress) in
                   self.progress = progress
               }).response(completionHandler: { (defaultResponse) in
                   self.respons = defaultResponse
               })
           }
       }
       self.state = ALDownloadState.Download
   }
复制代码

cancelledData 下载失败时的数据,恢复下载时需要用到 downloadRequest 每次下载时重新赋值,保证downloadRequest为最新值

取消下载

   func cancel() {
       downloadRequest?.cancel()
       self.state = ALDownloadState.Cancel
   }
复制代码

Alamofire的取消下载,加上下载状态的改变

挂起

   func hangup() {
       downloadRequest?.cancel()
       self.state = ALDownloadState.Wait
   }
复制代码

下载任务挂起(等待下载): 顺序下载时用到

同时下载

   func changeDownloadState() {
       self.downloadInfoArray = self.downloadInfoArray?.map({ (info) -> ALDownloadInfo in
           if  info.state == ALDownloadState.Download || info.state == ALDownloadState.Completed{}
           else{
               info.download()
           }
           return info
       })
   }
复制代码

正在下载和已经下载完成的任务保持,其余任务改变为下载状态

这里重点说下顺序下载

   func changeWaitState(completeClose: ALDownloadCompleteClose?) {
       self.completeClose = completeClose
           var isDownloadFirst = false
           self.downloadInfoArray = self.downloadInfoArray?.map({ (info) -> ALDownloadInfo in
               if isDownloadFirst == false {
                   if info.state == ALDownloadState.Download {
                   isDownloadFirst = true
                       return info
                   }
               }
               if info.state == ALDownloadState.Completed {}
               else{
                   info.hangup()
               }
               return info
           })
           if isDownloadFirst == false {
                  resumeFirstWillResume()
           }
   }
复制代码

如果有任务下载,下载中的第一个任务保持下载状态,其余改变为等待下载;如果没有任务下载,将所有任务改变为等待下载,再下载第一个等待任务,这是我的思路,有更好方法的麻烦告知我

就想要?? 欢迎戳这里 哈哈哈

发现bug或好的建议欢迎 issues or Email Yvente@163.com

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值