What is difference between NSURLSessionDataTask vs NSURLSessionDownloadTask

本小妞迷上赌 提交于 2019-12-03 10:48:00

问题


In latest apple introduce new NSURLSession in replace of NSURLConnection, so in there are different task , so what is the difference between NSURLSessionDataTask, NSURLSessionDownloadTask ?

and in which scenario use NSURLSessionTask and where NSURLSessionDownloadTask?


回答1:


NSURLSessionDataTask : Data tasks exchange data using NSData. NSURLSessionDataTask is not supported in Background Sessions.

Data tasks send and receive data using NSData objects. Data tasks are intended for short, often interactive requests from your app to a server. Data tasks can return data to your app one piece at a time after each piece of data is received, or all at once through a completion handler. Because data tasks do not store the data to a file, they are not supported in background sessions.

NSURLSessionDownloadTask : NSURLSessionDownloadTask directly writes the response data to a temporary file. It supports background downloads when the app is not running.

Download tasks retrieve data in the form of a file, and support background downloads while the app is not running.

I guess below image give you better knowledge:




回答2:


The docs answer this, but:

  • NSURLSessionDownloadTask downloads files to a disk, and you then save the resulting file somewhere to use later on.
  • NSURLSessionDataTask downloads files in memory, and it is up to you to determine how you want to handle the response.



回答3:


Adding to above answer

  • NSURLSessionDownloadTask It is possible to cancel a download task and resume it at a later point.

  • NSURLSessionDataTask We cant resume it for later.




回答4:


We can get it from header files

/* * An NSURLSessionDataTask does not provide any additional * functionality over an NSURLSessionTask and its presence is merely * to provide lexical differentiation from download and upload tasks. */

@interface NSURLSessionDataTask : NSURLSessionTask
@end

/* * An NSURLSessionUploadTask does not currently provide any additional * functionality over an NSURLSessionDataTask. All delegate messages * that may be sent referencing an NSURLSessionDataTask equally apply * to NSURLSessionUploadTasks. */

@interface NSURLSessionUploadTask : NSURLSessionDataTask
@end

/* * NSURLSessionDownloadTask is a task that represents a download to * local storage. */

@interface NSURLSessionDownloadTask : NSURLSessionTask


来源:https://stackoverflow.com/questions/20604910/what-is-difference-between-nsurlsessiondatatask-vs-nsurlsessiondownloadtask

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!