NSURLConnection:恢复功能
我正在用 Objective-C 编写一个下载管理器,并且它可以与恢复功能一起使用。我在收到数据时将其写入磁盘,这样如果下载因任何原因中断,它应该从中断的地方继续。 Apple 表示您应该期待一个或多个 - (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
事件。在这种情况下,他们说您应该重置之前收到的所有数据,因为您的下载正在被重定向,并且您已经收到的字节无效,因此我删除了现有文件并使用 0 字节重新创建。但是,如果我多次收到此事件,我必须删除以前以部分下载形式收到的数据,从而违背了恢复功能的目的。有解决办法吗?
我想出的解决方案是:仅重置我在第一个事件之后收到的 - (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
事件上的数据。这将解决大多数情况(我认为)。这在逻辑上合理吗?有更好的选择吗?文件下载触发多个 - (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
事件的可能性有多大?
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 previously 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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我的建议是使用已经解决了这个问题的人的库,ASIHttpRequest 就是我使用的。可以在此处找到该
信息 在该页面上搜索“正在恢复”
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
。删除迄今为止下载的所有内容是唯一明智的选择,当服务器告诉您扔掉某些内容并使用其他内容时,您无法继续下载某些内容。极不可能的。它仅用于某些类型的流媒体,不适用于您需要下载管理器的任何内容。
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.Extremely unlikely. It's only ever used in certain types of streaming, not for anything you'd need a download manager for.
这是一个漂亮而干净的库,我认为它具有您需要的功能:
https://github.com/Anviking/DownloadManager
This is nice and clean library that, I think, has functionality that you need:
https://github.com/Anviking/DownloadManager