Search code examples
iosswiftalamofire

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.

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)
                }
            }
    }
}

Solution

  • Earlier I was using Alamofire version 4.9 that time to cancel request I used following code

    request?.cancel()
    

    Using above line of code download request cancelled successfully and also we got resume data using response.resumeData.

    Then I changed Alamofire pod to latest version i.e. 5.4.4 And in new Alamofire version they changed previous functions and added more functions. So using following line of code I cancel request and also get resume data successfully.

    request?.cancel(producingResumeData: true)