Search code examples
objective-ciphoneios12

NSURLSessionDataTask, dataTaskWithRequest:completionHandler Issue in IOS12


In my Objective-C, iPhone App I was using NSURLSession successfully, my App started crashing with below error from iOS 12:

Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[NSConcreteMapTable dataTaskWithRequest:completionHandler:]: unrecognized selector sent to instance

To fetch Token ==>

+ (NSURLSession *)sharedSessionManager
{
    static NSURLSession *session = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
    NSURLSessionConfiguration *sessionConfiguration = [NSURLSessionConfiguration defaultSessionConfiguration];
    [sessionConfiguration setHTTPAdditionalHeaders:@{
                                                  @"Accept": @"application/json"
                                                  }
         ];
session = [NSURLSession sessionWithConfiguration:sessionConfiguration delegate:nil delegateQueue:nil];
    });
    return session;
}

Get saved token for All future calls ==>

+ (NSURLSession *)sharedAsyncSessionManager
{
    NSString *authToken = [NSString stringWithFormat: @"Bearer %@", m_strToken];
    static NSURLSession *session = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
    NSURLSessionConfiguration *sessionConfiguration =[NSURLSessionConfiguration defaultSessionConfiguration];
    [sessionConfiguration setHTTPAdditionalHeaders:@{
                                                         @"Content-Type": @"application/json",
                                                         @"Authorization": authToken}
         ];
        session = [NSURLSession sessionWithConfiguration:sessionConfiguration delegate:nil delegateQueue:nil];
    });
    return session;
}

Call API's ==>

NSURLSession *sessionMnger = [SessionManager sharedAsyncSessionManager];

NSURLSessionDataTask *task = [sessionMnger dataTaskWithRequest:request completionHandler:^(NSData *data, NSURLResponse *response, NSError *error)
  {
      NSMutableArray *arrTemp = [[NSMutableArray alloc]init];
  }

App crashes for dataTaskWithRequest in IOS 12. error message i can read as below:

Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[NSConcreteMapTable dataTaskWithRequest:completionHandler:]: unrecognized selector sent to instance

when ever i wanted to call API with above sessionMnger App crash....

anything missing from iOS 12 ?


Solution

  • Usually when you get "unrecognized selector sent to instance" error with unrelated class name (NSConcreteMapTable in this case) this signals memory management issue. Most likely original object is already released, memory is corrupted and runtime gets confused.

    To diagnose, try enabling "Zombie Objects" from Scheme menu, that should help identify the crash cause. Look for clues why memory corruption might be happening.