Search code examples
iosswiftalamofire

Setting DownloadRequest's URL


I'm developing a resume function for file downloads. It looks good for the most of it (getting the resume data of the cancelled request, etc.), but when I'm creating the request for resuming the download, its URL is nil.

The code for creating the request is pretty straightforward:

let downloadRequest = sessionManager.download(resumingWith: resumableData)

The downloadRequest object has a URLRequest variable but it is get-only.

I've read the doc but found no answer there.

Please note that the first request (the one that was cancelled) was created using this code:

let dataRequest = sessionManager.request(urlRequest)

How is the DownloadRequest object getting its url? Am I missing something obvious?


Solution

  • The answer lies in the first request made. When first starting to download the file, even if you don't plan to resume it later, you need to create an Alamofire.DownloadRequest (URLSessionDownloadTask) and not an Alamofire.DataRequest (URLSessionDataTask). The rest is the same: you capture the resume data when it fails (or if you cancel it by producing resume data), then you create the next request with that data.

    Hope this helps!