[英]Should I choose URLSessionDataTask or URLSessionDownloadTask for getting image
I use URLSession
and URLSessionDataTask
to get an image from server and display it in an app.我使用
URLSession
和URLSessionDataTask
从服务器获取图像并将其显示在应用程序中。 I had a look here .我看了一下这里。 It looks like
URLSessionDownloadTask
has more options.看起来
URLSessionDownloadTask
有更多选项。
Currently I use the following code for getting the image:目前我使用以下代码获取图像:
let task = URLSession.shared.dataTask(with: url) {(data, response, error) in
guard error == nil else {
completion(error, nil)
return
}
completion(nil, data)
}
task.resume()
I'd like to be able to suspend, cancel and resume the process of getting the image from the server.我希望能够暂停、取消和恢复从服务器获取图像的过程。 I see in the documentation that
URLSessionDataTask
also has these options.我在文档中看到
URLSessionDataTask
也有这些选项。 But it's also written for the suspend method of URLSessionTask
that:但它也是为
URLSessionTask
的暂停方法编写的:
A download task can continue transferring data at a later time.
下载任务可以在以后继续传输数据。 All other tasks must start over when resumed.
所有其他任务必须在恢复时重新开始。
So my question is: Should I change the implementation to use URLSessionDownloadTask
for getting the images if I need to be able to stop getting the image at some point and resume later without losing the current progress?所以我的问题是:如果我需要能够在某个时候停止获取图像并稍后恢复而不丢失当前进度,我是否应该更改实现以使用
URLSessionDownloadTask
获取图像? Thank you in advance.先感谢您。
Yes, if you want to resume a suspended task, a download task lets it resume from where it left off.是的,如果您想恢复暂停的任务,下载任务可以让它从中断的地方恢复。
Other reasons you might want to use download tasks include that:您可能想要使用下载任务的其他原因包括:
the peak memory usage is lower, as download tasks write data to the temporary file as it goes along, whereas data tasks hold the complete resource in memory;峰值 memory 使用率较低,因为下载任务将数据写入临时文件,而数据任务在 memory 中保存完整资源; and
和
with download tasks, you can use URLSessionConfiguration.background
so that the download continues even after the user leaves your app.对于下载任务,您可以使用
URLSessionConfiguration.background
以便在用户离开您的应用程序后继续下载。
NSURLSessionDataTask : Data tasks exchange data using NSData. NSURLSessionDataTask :数据任务使用 NSData 交换数据。 NSURLSessionDataTask is not supported in Background Sessions because it does not write the content in a form of a local file(stored in memory).
后台会话不支持 NSURLSessionDataTask,因为它不会以本地文件的形式(存储在内存中)写入内容。 Therefore it can not be resumed later onwards.
因此,它不能在以后恢复。
NSURLSessionDownloadTask : NSURLSessionDownloadTask directly writes the response data to a temporary file. NSURLSessionDownloadTask : NSURLSessionDownloadTask 直接将响应数据写入一个临时文件。 It supports background downloads when the app is not running and in your case does allow resume of downloads.
它在应用程序未运行时支持后台下载,并且在您的情况下允许继续下载。
The question is why do you want to resume the download of an "image" you are trying to display in the App.问题是您为什么要继续下载您尝试在应用程序中显示的“图像”。 Will it ever change?
它会改变吗? or will it always be the same through out the App.
还是在整个应用程序中总是一样的。 If it has a chance of changing in the future i think you should stick to
URLSessionDataTask
because imo it will eat up local storage to download and write images over and over again.如果它有机会在未来发生变化,我认为你应该坚持使用
URLSessionDataTask
,因为 imo 它会消耗本地存储来一遍又一遍地下载和写入图像。
The risk of using NSURLSessionDownloadTask is that prior to download you will have to check if the download space available on the device is sufficient to go ahead with, in other words it is a must to handle fileSize errors as apple thinks its upto the developer to meet those requirements使用NSURLSessionDownloadTask的风险在于,在下载之前,您必须检查设备上的可用下载空间是否足以支持 go,换句话说,必须处理 fileSize 错误,因为苹果认为这取决于开发人员那些要求
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.