暂停和恢复 ASINetworkQueue
我正在使用 ASINetworkQueue 在 iPad 应用程序中下载大约 50 个文件。我正在寻找一种允许用户暂停和恢复队列的方法。
ASIHTTP 文档提到,
[request setAllowResumeForFileDownloads:YES];
但这在单个请求级别运行,而不是在队列级别运行。由于 ASINetworkQueue 是 NSOperationQueue 的子类,我也尝试过
[queue setSuspended:YES];
,虽然这会暂停队列,但它不会影响正在进行的下载,它只是等待下载完成,然后暂停队列,在我的情况下这意味着很多秒在用户按下按钮和队列实际暂停之间,这不是我想要的 UI 体验。
谁能建议另一种方法来解决这个问题?
I'm using an ASINetworkQueue to download around 50 files in an iPad app. I'm looking for a way of allowing the user to pause and resume the queue.
The ASIHTTP docs refer to
[request setAllowResumeForFileDownloads:YES];
but this operates at an individual request level, not at the queue level. As ASINetworkQueue is a subclass of NSOperationQueue I've also tried
[queue setSuspended:YES];
and while this will pause a queue, it does not affect the downloads in progress, it just waits until they've finished and then pauses the queue, which in my case means many seconds between the user pressing the button and the queue actually pausing, which is not the UI experience I want.
Can anyone suggest another way of solving this problem?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我的解决方案..
首先,暂停函数取消所有正在运行的操作,并重新创建新请求将它们推入队列。
然后恢复函数取消暂停队列。
注意,请求不要设置setAllowResumeForFileDownloads:YES,否则totalBytesToDownload会计算错误。如果allowResumeForFileDownloads=NO,其值将与队列中的请求数相同。
这是我的请求失败处理程序,我在文件下载失败时添加重试。但我不希望取消请求时会调用重试机制,因此我将 userInfo(ignore:true) 设置为 request 对象以防止发生这种情况。
不知道有没有更好的解决办法,希望可以帮到你。
My solution..
First, pause function cancel all running operations, and recreate new requests push them into queue.
Then resume function unsuspends the queue.
Be noticed, request should not set setAllowResumeForFileDownloads:YES, otherwise the totalBytesToDownload will calculate wrong..If allowResumeForFileDownloads=NO, its value will be same as the count of requests in queue.
Here is my request fail handler, I add retry when file download fail. But I don't whant when I cancel a request, the retry mechanism will be invoked, so I set userInfo(ignore:true) to request object to prevent it happens.
I am not sure if there is a better solution, hope it can help you.