NSURLSession post: difference between uploadTask and dataTask

These are my two examples:

let config = NSURLSessionConfiguration.defaultSessionConfiguration() config.HTTPAdditionalHeaders = ["Accept": "application/json", "Content-Type": "application/json", "User-Agent": UIDevice.currentDevice().model] var request = NSMutableURLRequest(URL: NSURL(string: "http://XXX")) request.HTTPMethod = "POST" let valuesToSend = ["key":value, "key2":value] var error: NSError? let data = NSJSONSerialization.dataWithJSONObject(valuesToSend, options:NSJSONWritingOptions.PrettyPrinted, error: &error) request.HTTPBody = data if error == nil { let task = NSURLSession(configuration: config).dataTaskWithRequest(request, completionHandler: {data, response, error in if error == nil { println("received == \(NSString(data: data, encoding: NSUTF8StringEncoding))") } }) task.resume() } else { println("Oups error \(error)") } 

And second

 let config = NSURLSessionConfiguration.defaultSessionConfiguration() config.HTTPAdditionalHeaders = ["Accept": "application/json", "Content-Type": "application/json", "User-Agent": UIDevice.currentDevice().model] var request = NSMutableURLRequest(URL: NSURL(string: "http://XXX")) request.HTTPMethod = "POST" let valuesToSend = ["key":value, "key2":value] var error: NSError? let data = NSJSONSerialization.dataWithJSONObject(valuesToSend, options:NSJSONWritingOptions.PrettyPrinted, error: &error) if error == nil { let task = NSURLSession(configuration: config).uploadTaskWithRequest(request, fromData: data, completionHandler: {data, response, error in if error == nil { println("received == \(NSString(data: data, encoding: NSUTF8StringEncoding))") } }) task.resume() } else { println("Oups error \(error)") } 

So, I wonder: what are the differences between the two examples and what is the best for my case (simple post and reception)

Are both in the background? So?

+7
post ios background-process nsurlsession
source share
1 answer

From NSURLSession Link :

dataTaskWithRequest:

Creates an HTTP request based on the specified URL request object. - (NSURLSessionDataTask *)dataTaskWithRequest:(NSURLRequest *)request Parameters

Request

An object that provides request-specific information, such as a URL, cache policies, request type, and body or body stream data.

Return value

New session data task.

Discussion

After creating the task, you should start it by calling its resume Method.

Availability

Available in iOS 7.0 and later.

Announced in

NSURLSession.h


uploadTaskWithRequest:fromData:

Creates an HTTP request for the specified URL request object and loads the provided data object. - (NSURLSessionUploadTask *)uploadTaskWithRequest:(NSURLRequest *)request fromData:(NSData *)bodyData Parameters

Request

NSURLRequest object that provides a URL, cache policy, request type, etc. Body and body stream data in this request object is ignored.

bodyData p>

Body data for the request.

Return value

New session load task.

Discussion

After creating the task, you should start it by calling its resume Method.

Availability

Available in iOS 7.0 and later.

Announced in

NSURLSession.h

In addition, Ray Wenderlich says:

NSURLSessionDataTask

This task issues HTTP GET requests for uploading data from servers. data is returned in the form of NSData . Then you would convert this data to the correct type XML , JSON , UIImage , plist , etc.

 NSURLSessionDataTask *jsonData = [session dataTaskWithURL:yourNSURL completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) { // handle NSData }]; 

NSURLSessionUploadTask

Use this class when you need to upload something to a web service using the HTTP POST or PUT . The task delegate also allows you to view network traffic during transmission.

Upload image:

 NSData *imageData = UIImageJPEGRepresentation(image, 0.6); NSURLSessionUploadTask *uploadTask = [upLoadSession uploadTaskWithRequest:request fromData:imageData]; 

Here the task is created from the session and the image is loaded as NSData . There are also ways to download using a file or stream.

However, your question remains rather ambiguous and too broad, since you did not explain the explicit specific problem, and you can easily find this information after a little work.

+6
source share

All Articles