Question

I'm working now on a NetworkCommunicator Helper class, that will handle my connection to the server,i'm using the NSURLSession API and i have a question about the creation of the NSURLSession tasks. There are two ways to create a taks:

1 - with a NSURLRequest

2 - with a URL

I was wondering what is the preferred way? to be more specific in what way will my life be much easier (adding headers, setting verb types, etc'..).

Thanks

Was it helpful?

Solution

You are much more flexible when you use methods that take NSURLRequest like for example dataTaskWithRequest: method. That way you can customize HTTP method, request body, headers, basically every parameter of the NSURLRequest because it is you that creates it.

Methods that take NSURL create the NSURLRequest for you under the hood so you cannot modify the request afterwards. For example dataTaskWithURL: method creates an HTTP GET request for the specified URL and you cannot change it to POST.

Example of creating a task with NSURLRequest. You can see you can be very flexible here:

// Create a simple JSON data.
NSData *JSONData = [NSJSONSerialization dataWithJSONObject:@{ @"numbers" : @[@1, @2, @3] } options:0 error:nil];

// Create a POST request with our JSON as a request body.
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];

// Set HTTP method.
request.HTTPMethod = @"POST";

// Set request body.
request.HTTPBody = JSONData;

// Create a task.
NSURLSessionDataTask *task = [[NSURLSession sharedSession] dataTaskWithRequest:request
                                                             completionHandler:^(NSData *data,
                                                                                 NSURLResponse *response,
                                                                                 NSError *error)
{
    ...
}];

Example of creating a task using NSURL. Here you cannot modify the request. It is created for you.

// Create a GET task.
NSURLSessionDataTask *task = [[NSURLSession sharedSession] dataTaskWithURL:url
                                                         completionHandler:^(NSData *data,
                                                                             NSURLResponse *response,
                                                                             NSError *error)
{
 ...
}];

OTHER TIPS

I don't think there is a preferred way, or at least is not explicit by Apple.

The question is more about how much flexibility do you want to provide to the caller, allowing the caller to use an NSURLRequest means that the caller can do more customisation on the request and for example the called is able, by setting the right property on the NSURLRequest, to override some configuration done in the NSURLSessionConfiguration.

I think in general term you want to provide both; the one that allow to set an NSURL is proxy through the one that will use the NSURLRequest. Basically the NSURL method will be just a convenience method for the caller, if the caller is not interested in do a fine grain setup of the request it will send you an NSURL otherwise will set you an NSURLRequest.

If you designing this kind of API a general advice that I can give you is to always to a copy to have the immutable version of the parameter you receive. This is because the caller can pass you a mutable object (NSMutableURLRequest) and you don't want to allow the caller to mess around with your internals.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top