如何从完成的`NSURLSessionDataTask`中获取数据? [英] How do I get the data from a finished `NSURLSessionDataTask`?

查看:498
本文介绍了如何从完成的`NSURLSessionDataTask`中获取数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道我可以使用 dataTaskWithURL:completionHandler:来获取completionHandler块中的数据,但是阻止了委托方法的触发,我需要 didReceiveData:触发方法,因为它是我配置进度指示器的方式。

I know I can use dataTaskWithURL:completionHandler: to get the data in the completionHandler block, but that blocks the delegate methods from firing, and I need the didReceiveData: method to fire, as it's how I configure my progress indicator.

我完全不知道如何获取下载的数据一旦完成。什么是委托方法相当于完成块? didCompleteWithError 似乎没有返回任何 NSData

I'm completely at a loss how to get the downloaded data once it's complete. What's the delegate method equivalent of the completion block? didCompleteWithError doesn't seem to return any NSData.

我不必在 didReceiveData 中手动分割数据,是吗?当完成处理者把它交给你时,这似乎真的很蹩脚。我不介意这样做,如果不是因为我可以同时下载50多个东西,所以跟踪所有部分数据似乎是屁股的痛苦。我应该切换到 NSURLSessionDownloadTask

I don't have to manually piece the data together in didReceiveData, do I? That seems really lame when the completionHandler just hands it off to you. I wouldn't mind doing that if it weren't for the fact that I could be downloading 50+ things at once, so keeping track of all that partial data seems like a pain in the ass. Should I just switch to NSURLSessionDownloadTask?

推荐答案

是的,你必须手动将数据拼凑在一起(或者你可以将它流式传输到文件中,如果它真的很大并且你不想占用内存)。

Yes, you have to manually piece the data together (or you can stream it to a file if it's really big and you don't want it taking up memory).

所以, didReceiveData 方法将返回你的数据。所以你应该实例化一个 NSMutableData (例如,在<$ c中) $ c> didReceiveResponse ) didReceiveData 将附加数据。当 didCompleteWithError ,假设错误 nil ,您可以确信您的 NSMutableData 现在包含所有数据接收。正如您所指出的,挑战在于跟踪所有50多个下载,因此我维护一个由任务标识符键入的字典,以跟踪将数据附加到哪个字典。 (就个人而言,我认为这是一个设计缺陷, NSURLSession 在会话级实现任务,下载和上传委托,而不是让我们为每个任务实例化单独的任务委托对象但是我们仍然坚持我们所拥有的。)

So, didReceiveData method will be returning your data as it comes in. So you should have instantiated a NSMutableData (for example, in didReceiveResponse) to which didReceiveData will append the data as it comes in. When didCompleteWithError is called, assuming the error is nil, you can be confident that your NSMutableData now contains all of the data received. As you noted, the challenge is keeping track of all of the 50+ downloads, so I maintain an dictionary keyed by task identifiers to keep track of which to append the data to. (Personally, I think it's a design flaw that NSURLSession implements the task, download, and upload delegates at the session level, rather than letting us instantiate separate task delegate objects for each task. But we're stuck with what we've got.)

如果您只是下载数据,那么 NSURLSessionDownloadTask 是一个很好的选择(并且在内存使用方面更有效,而不仅仅是附加到 NSMutableData 实例),如果你想要,你可以想象也使用后台会话(你不能用 NSURLSessionDataTask )。

If you're just downloading the data, the NSURLSessionDownloadTask is a great alternative (and is more efficient in terms of memory usage than just appending to NSMutableData instances), and you can conceivably also use a background session if you want (which you can't with a NSURLSessionDataTask).

最后,如果你真的做了50多次下载,您可能需要考虑将下载任务包装在 NSOperation 子类中,这样您就可以限制并发运行的数量,而不会有任何超时的风险。

Finally, if you're really doing 50+ downloads, you might want to consider wrapping the download tasks in NSOperation subclass so you can constrain how many run concurrently without risking having any timeout.

这篇关于如何从完成的`NSURLSessionDataTask`中获取数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆