Significant Decrease in Network Speed After Replacing NSURLConnection with NSURLSession

Hello,

I recently replaced NSURLConnection with NSURLSession in my application, and I have noticed a significant decrease in network speed. I am seeking advice on why this might be happening and how to resolve the issue.

Here is the code before the change:

Old Code:

- (void)execute:(id<STHttpEntity>)entity
{
  [entity addHeader:API_HDR_CLASS_NAME value:self.apiClass];
  [entity addHeader:API_HDR_METHOD_NAME value:self.apiMethod];

  NSMutableURLRequest *req = [self createRequest:entity];
  self.connection = [[NSURLConnection alloc]
                      initWithRequest:req
                             delegate:self];
  [self.connection start];
}

- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
  [self clearConnectionTimeout];
  self.requestData = nil;
  if (self.httpStatus != HTTPSTATUS_OK) {
    [self callFailedWithStatus:self.httpStatus];
    return;
  }
  [self callSucceeded];
}

And here is the code after the change:

New Code:

- (void)execute:(id<STHttpEntity>)entity
{
  [entity addHeader:API_HDR_CLASS_NAME value:self.apiClass];
  [entity addHeader:API_HDR_METHOD_NAME value:self.apiMethod];

  NSMutableURLRequest *req = [self createRequest:entity];
  NSURLSessionConfiguration *sessionConfig = [NSURLSessionConfiguration defaultSessionConfiguration];
  NSURLSession *session = [NSURLSession sessionWithConfiguration:sessionConfig delegate:self delegateQueue:nil];
  self.dataTask = [session dataTaskWithRequest:req];
  [self.dataTask resume];
}

- (void)URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task didCompleteWithError:(nullable NSError *)error
{
  [self clearConnectionTimeout];
  self.requestData = nil;
  if (error) {
    [self callFailed:error];
  } else {
    [self callSucceeded];
  }
}

Issue: After replacing NSURLConnection with NSURLSession, the network speed has significantly decreased. The new implementation seems to be much slower than the old one.

Questions:

1.What could be the reasons for the significant decrease in network speed after switching to NSURLSession?

2.Are there any specific configurations or best practices for NSURLSession that I should be aware of to improve performance?

3.Is there any known issue with NSURLSession that could cause such a performance drop?

Any insights or suggestions would be greatly appreciated. Thank you in advance for your help!

NSURLSession and NSURLConnection are implemented on top of the same core. I wouldn’t expect to see a significant performance difference between the two.

What platform is this on?

What sort of performance drop are we talking about here? Are you issuing large requests, and thus seeing a bandwidth drop? Or small requests and thus a latency drop?

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"

Significant Decrease in Network Speed After Replacing NSURLConnection with NSURLSession
 
 
Q