29

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?

Toseef Khilji
  • 16,458
  • 10
  • 78
  • 115

5 Answers5

57

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:

enter image description here

Toseef Khilji
  • 16,458
  • 10
  • 78
  • 115
  • I finally have preferred to use `NSURLSessionDownloadTask` as we can have additional features with it.. – NSPratik Jan 28 '16 at 14:15
  • *Because data tasks do not store the data to a file* what does this mean?! They don't store it to a file?! Then where is being stored? What does form of a file mean? I feel like that's the heart of your answer which has some other prerequisite that I don't understand. Perhaps if you share real world examples then understanding would become easier... – Honey Dec 28 '16 at 16:25
  • @Honey NSURLSessionDataTask stores it in the memory. – CodeOverRide Nov 13 '19 at 00:09
20

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.
zadr
  • 2,390
  • 17
  • 18
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.

garg
  • 2,344
  • 20
  • 20
  • is this only the difference? It seems more to it than that. for instance, NSURLSessionDownloadTask will download file etc. when the app is closed while NSURLSessionDataTask doesn't. – CodeOverRide Nov 13 '19 at 00:05
0

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
Victor Choy
  • 3,281
  • 21
  • 30
0

Adding to the previous answers: Apple made it clear in their guide "URL Loading System"

For small interactions with remote servers, you can use the URLSessionDataTask class to receive response data into memory (as opposed to using the URLSessionDownloadTask class, which stores the data directly to the file system). A data task is ideal for uses like calling a web service endpoint.

https://developer.apple.com/documentation/foundation/url_loading_system/fetching_website_data_into_memory#overview

Pierre
  • 380
  • 4
  • 10