1

I'm fairly new to iOS development and I've been stuck on this bug for a while. I'm making a simple app the uses a web service. Right now I currently have two view controllers. A login view controller (with its NIB file) and a main view controller (with its NIB file). When I created the app I chose an empty application so I don't have a storyboard. Instead I'm using UINavigationController. When I run my code I get the following error after entering my username and password and pressing submit in the login view:

Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: '-[UIKeyboardTaskQueue waitUntilAllTasksAreFinished] may only be called from the main thread.'

This is the code I have for my submit button:

-(IBAction)logIn:(id)sender{
    UIApplication *application = [UIApplication sharedApplication];
    application.networkActivityIndicatorVisible = YES;

    [_loginNetworkingContorller checkCredentialsWithUsername:self.username.text withPassword:self.password.text completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
        if(!error){
            NSHTTPURLResponse *httpResp = (NSHTTPURLResponse*) response;
            if (httpResp.statusCode == 200) {
                NSLog(@"SUCESS");
                NSDictionary *credentials = @{self.username.text: self.password.text};
                [KeychainUserPass save:@"MY APP" data:credentials];
                UIViewController *mainView = [[RDMainViewController alloc] initWithNibName:@"RDMainViewController" bundle:nil];

                [self.navigationController pushViewController:mainView animated:YES];
            }
            else{
                NSLog(@"ERROR");
            }
        }
        else{
            NSLog(@"ERROR");
        }
    }];
}

And here is the code for the following function

[_loginNetworkingContorller checkCredentialsWithUsername:self.username.text withPassword:self.password.text completionHandler:^(NSData *data, NSURLResponse *response, NSError *error)

-(void)checkCredentialsWithUsername:(NSString *)username withPassword:(NSString *)password completionHandler:(void (^)(NSData *data,NSURLResponse *response, NSError *error))myCompletion
{
    NSString *requestString = @"SOME WEBSITE";
    NSURL *url = [NSURL URLWithString:requestString];
    NSURLRequest *req = [NSURLRequest requestWithURL:url];

    NSData *userPasswordData = [[NSString stringWithFormat:@"%@:%@", username, password] dataUsingEncoding:NSUTF8StringEncoding];
    NSString *base64EncodedCredential = [userPasswordData base64EncodedStringWithOptions:0];
    NSString *authString = [NSString stringWithFormat:@"Basic %@", base64EncodedCredential];

    NSURLSessionConfiguration *sessionConfig=[NSURLSessionConfiguration defaultSessionConfiguration];
    sessionConfig.HTTPAdditionalHeaders=@{@"Authorization":authString};

    self.session=[NSURLSession sessionWithConfiguration:sessionConfig];

    NSURLSessionDataTask *dataTask = [self.session dataTaskWithRequest:req completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
        myCompletion(data, response, error);
    }];

    [dataTask resume];
}

I'm a stuck right now and am not really sure what the issue is especially since I don't do anything with the keyboard. I have a feeling there is an issue with my blocks but I'm not exactly sure what the issue is. Any help would be greatly appreciated.

user2604504
  • 697
  • 2
  • 14
  • 29

1 Answers1

2

Hey I had this same issue after I received my response from my web call. I was able to solve it be doing the following:

 //do something with response

  dispatch_async(dispatch_get_main_queue()) { () -> Void in

     // continue with program by calling next step on main thread           
  }

I think if you push to the next view controller iOS attempts to do it on not the main thread, causing the error, but I'm not 100% is that is accurate

Bhavin Bhadani
  • 22,224
  • 10
  • 78
  • 108
Raymond
  • 21
  • 2