连接断开时从头开始恢复Alamofire下载

时间:2018-08-29 04:04:08

标签: swift alamofire afnetworking nsurlsession resume

这是我用alamofire进行的下载功能,我已经注意到连接丢失,例如关闭wifi响应块不会产生任何恢复数据,该怎么办? 下载失败,恢复数据始终为

func StartDownload () {

    let fileUrl = self.getSaveFileUrl(fileName: Data[0] as String)
    let destination: DownloadRequest.DownloadFileDestination = { _, _ in
        return (fileUrl, [.removePreviousFile, .createIntermediateDirectories])
    }

    downloadRequest = Alamofire.download(Data[0] as String , to:destination)

        .downloadProgress { (progress) in


            self.progressCircle.progress = progress.fractionCompleted
          }

        .validate()

        .response { defaultDownloadResponse in

            self.Timeer?.invalidate()

            if  (defaultDownloadResponse.error != nil) || defaultDownloadResponse.resumeData != nil  {
                self.startButton.isHidden = false
                self.pauseButton.isHidden = true
                print("someThing went wrong \(String(describing: defaultDownloadResponse.error))")

                print(defaultDownloadResponse.resumeData as Any)

                if let resumeDataa = defaultDownloadResponse.resumeData {
                    let Resume = CachableData(urll: self.Data[0].lastPathComponent as String, Resume: resumeDataa)
                    self.cacher.persist(item: Resume) { url, error in
                        if let error = error {
                            print("Text failed to persist: \(error)")
                        } else {
                            print("Text persisted in \(url?.absoluteString ?? "m")")
                            return
                        }
                    }

                }
            }


    }
    }
}

0 个答案:

没有答案