I have a simple post service which takes a json as input and gives a json as output. When I am using NSURLConnection, I get the response within 500ms. But if I use NSURLSession it takes atleast 4-5s to respond for the same request.
Also with NSURLSession didCompleteWithError always firing though there is no error and error is nil. But in case of NSURLConnection didFailWithError is tot firing.
Please tell me what I am doing wrong with NSURLSession.
POST using NSURLConnection
-(void) postData: (NSString *)requestBody toAddress: (NSString *)url{
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:url]];
[request setHTTPMethod:@"POST"];
[request setValue:[NSString stringWithFormat:@"%lu",(unsigned long)requestBody.length] forHTTPHeaderField:@"Content-Length"];
[request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
[request setHTTPBody:[requestBody dataUsingEncoding:NSUTF8StringEncoding]];
NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
}
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)aResponse
{
data = [NSMutableData data];
[data setLength:0];
}
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)bytes
{
[data appendData:bytes];
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
NSLog(@"Response http status code %ld", (long)[httpResponse statusCode]);
NSLog(@"response %@",[data getString]);
}
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
NSLog(@"Error in service");
}
POST using NSURLSession
-(void) postData: (NSString *)requestBody toAddress: (NSString *)url{
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:url]];
[request setHTTPMethod:@"POST"];
[request setValue:[NSString stringWithFormat:@"%lu",(unsigned long)requestBody.length] forHTTPHeaderField:@"Content-Length"];
[request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
[request setHTTPBody:[requestBody dataUsingEncoding:NSUTF8StringEncoding]];
NSURLSessionConfiguration *defaultConfigObject = [NSURLSessionConfiguration ephemeralSessionConfiguration];
NSURLSession *defaultSession = [NSURLSession sessionWithConfiguration: defaultConfigObject delegate: self delegateQueue: [NSOperationQueue mainQueue]];
NSURLSessionDataTask *dataTask = [defaultSession dataTaskWithRequest:request];
[dataTask resume];
}
- (void)URLSession:(NSURLSession *)session dataTask:(NSURLSessionDataTask *)dataTask
didReceiveResponse:(NSURLResponse *)response
completionHandler:(void (^)(NSURLSessionResponseDisposition disposition))completionHandler
{
httpResponse = (NSHTTPURLResponse*)response;
completionHandler(NSURLSessionResponseAllow);
}
- (void)URLSession:(NSURLSession *)session dataTask:(NSURLSessionDataTask *)dataTask
didReceiveData:(NSData *)data
{
NSLog(@"Response http status code %ld", (long)[httpResponse statusCode]);
NSLog(@"response %@",[data getString]);
}
- (void)URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task
didCompleteWithError:(NSError *)error
{
if (error != nil)
{
NSLog(@"Error in service call");
}
}