开发者

NSURLConnection: Resume functionality

开发者 https://www.devze.com 2023-02-14 17:42 出处:网络
I\'m writing a download manager in Objective-C, and I have it working with resume functionality. I am writing the data to disk as I receive it so that if the download is interrupted for any reason, it

I'm writing a download manager in Objective-C, and I have it working with resume functionality. I am writing the data to disk as I receive it so that if the download is interrupted for any reason, it should pick up where it left off. Apple says you should expect one or more - (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response events. In this event, they say you should reset any data you have previously received because your download is being redirected and the bytes you have already received is invalid, so I delete the existing file and recreate with 0 bytes. But if I receive this event multiple times, I have to delete data I have prev开发者_运维知识库iously received in the form of a partial download, defeating the purpose of resume functionality. Is there a solution to this?

The solution I have come up with is: only reset the data on- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response events I receive after the first. This would fix the majority of cases (I would think). Is this logically sound? Is there a better alternative? How likely is a file download to fire multiple - (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response events?


My recommendation would be to use a library from someone who has already solved this problem, ASIHttpRequest is what I use. It can be found here

Search for 'Resuming' on that page


multipart/x-mixed-replace is used when the server wants to replace what it has already given you with something else. Deleting everything you've downloaded so far is the only sensible option, you can't resume downloading something when the server is telling you to throw it away and use something else instead.

How likely is a file download to fire multiple - (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response events?

Extremely unlikely. It's only ever used in certain types of streaming, not for anything you'd need a download manager for.


This is nice and clean library that, I think, has functionality that you need: https://github.com/Anviking/DownloadManager

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号