Alamofire: How to continue download file?

Created on 9 Mar 2016  路  11Comments  路  Source: Alamofire/Alamofire

I want continue download After I press 'startDownload' button, (Use resumeData)
when I second time enter app after app terminated.

Here is my Example code:
https://github.com/wgywgy/AlamofireDownloadDemo/tree/master

where to find resumeData?

support

Most helpful comment

Hi @wgywgy,

You'll want to use the request.cancel() API to generate the resumeData which you will then extract as shown in this example in the README. Then you'll need to use the resumeData and the resume API to resume the request the next time you hit startDownload.

In the future, these types of questions are best suited for Stack Overflow @wgywgy. We use our GitHub project for feature requests and bug reports.

Best of luck! 馃嵒


From our Contribution Guidelines

Asking Questions

We don't use GitHub as a support forum. For any usage questions that are not specific to the project itself, please ask on Stack Overflow instead. By doing so, you'll be more likely to quickly solve your problem, and you'll allow anyone else with the same question to find the answer. This also allows maintainers to focus on improving the project for others.

All 11 comments

Hi @wgywgy,

You'll want to use the request.cancel() API to generate the resumeData which you will then extract as shown in this example in the README. Then you'll need to use the resumeData and the resume API to resume the request the next time you hit startDownload.

In the future, these types of questions are best suited for Stack Overflow @wgywgy. We use our GitHub project for feature requests and bug reports.

Best of luck! 馃嵒


From our Contribution Guidelines

Asking Questions

We don't use GitHub as a support forum. For any usage questions that are not specific to the project itself, please ask on Stack Overflow instead. By doing so, you'll be more likely to quickly solve your problem, and you'll allow anyone else with the same question to find the answer. This also allows maintainers to focus on improving the project for others.

I still don't understand how to get resumeData. I know how to continue download after pause without App terminate, but how to get resumeData when user terminate app and second time come in.
Can you Give me a Exmaple? Thank you.

I got the same problem, I save the resume data before cancel()

Alamofire.download(resumeData: cancelledData, destination: self.destination)
and when reopen the App It runs well in iOS 9 but doesn't work in iOS 8(simulator), saying: error 2 opening resume file

The resumeData is not guaranteed to actually resume @jdleung. You want to try it, and if the OS cannot resume the download, it will restart it instead.

@cnoon OK, Thx.

Hello @cnoon,

sorry to use this closed thread to ask you something but last time I have tried to use the stackoverflow with the _Alamofire_ tag and no one answered me 馃槥

So, I think I am facing the same problem as they were. I did understand what you have said and how to proceed if the app is still running when the download is cancelled. However, when the app is killed (by user interaction or crash) idk how to store the resumeData to resume the request when the app starts again. Any thoughts on how I can implement this when that happens?

Thanks

@tiagoaap If the user kills your app there is no way to take any action, much less save resume data. At best you could manually write the download data to disk periodically during the download, but that's a lot of work for a user who doesn't care if your app completes the download.

@jshier I have found a workaround. Probably it is not the most elegant solution but if the user kills the app, I use the applicationWillTerminate to simulate a cancel(byProducingResumeData completionHandler: @escaping (Data?) -> Void). Thus, I am able to store the resumeData in order to resume the download when the app starts again.

@tiagoaap I'd be very surprised if that delegate method was called when the user manually killed the app.

@jshier is correct, that delegate API will not be called in the event that the "user" force quits your app. That API is only called if "iOS" is going to quit your app when it's backgrounded b/c the device needs more memory.

@tiagoaap I'd be very surprised if that delegate method was called when the user manually killed the app.

I'm also surprised because same api was not working for me.

_task.cancel()_ works.

Was this page helpful?
0 / 5 - 0 ratings