简体   繁体   English

如果下载请求被取消,resumeData 总是返回 nil

[英]The resumeData always returns nil if the download request is cancelled

I am downloading data using Alamofire downloadRequest but in some scenarios I am cancelling downloadRequest and store data using resumeData in Alamofire version 4.5 but now I updated pod to latest Alamofire 5.4.4 version and now I am getting resumeData nil when I cancel downloadRequest.我正在使用 Alamofire downloadRequest 下载数据,但在某些情况下,我正在取消 downloadRequest 并在 Alamofire 4.5 版中使用 resumeData 存储数据,但现在我将 pod 更新到最新的 Alamofire 5.4.4 版本,现在我在取消 downloadRequest 时得到 resumeData nil。

Follow sharing code关注分享码

 static func downloadUserGuide(_ downloadLink: String, _ completion:@escaping(Bool) -> Void) {
 if let data = UserDefaults.standard.data(forKey: downloadLink) {
 resumeDownloadUserGuide(resumeData: data, userGuide.usermanual, { isDownloaded in
            completion(isDownloaded)
        })
}else{
self.request =  AF.download(downloadLink, to: kDestination)
            .downloadProgress { progress in
                Console.log("Download Progress: \(progress.fractionCompleted)")
            }
.responseData { response in
                if response.value != nil {
                    if !self.userGuide.isInvalidated {
                        try! realm.write {
                            self.userGuide.pdfLinkURL = response.fileURL?.path ?? ""
                        }
                    }
                    saveUserGuideInDB(self.userGuide)
                    Console.log("Download successfull")
                    UserDefaults.standard.removeObject(forKey: downloadLink)
                    completion(true)
                }else if response.error != nil {
                    if let resumeData = response.resumeData {
                        UserDefaults.standard.set(resumeData, forKey: downloadLink)
                    }
                    completion(false)
                }
            }
    }
}

Earlier I was using Alamofire version 4.9 that time to cancel request I used following code早些时候我当时使用的是 Alamofire 4.9 版来取消请求我使用了以下代码

request?.cancel()

Using above line of code download request cancelled successfully and also we got resume data using response.resumeData.使用上面的代码行下载请求成功取消,我们还使用 response.resumeData 获得了恢复数据。

Then I changed Alamofire pod to latest version ie 5.4.4 And in new Alamofire version they changed previous functions and added more functions.然后我将 Alamofire pod 更改为最新版本,即 5.4.4 在新的 Alamofire 版本中,他们更改了以前的功能并添加了更多功能。 So using following line of code I cancel request and also get resume data successfully.因此,使用以下代码行我取消请求并成功获取恢复数据。

request?.cancel(producingResumeData: true)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM