Question

So I have been trying to create an app for a website and I've got the "Log in" page working except when it won't transition to the next view.

This is the code that I believe is causing the problem :

NSOperationQueue *queue = [[NSOperationQueue alloc] init];
[NSURLConnection sendAsynchronousRequest:request queue:queue completionHandler:^(NSURLResponse *response, NSData *data, NSError *error)
 {
     NSString *str=[[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding];
     //NSLog(@"%@", str);
     if ([str rangeOfString:@"The username or password you provided is invalid. Please try again."].location == NSNotFound) {
         loginPageStatusLabel.text = @"Correct";
         NSLog(@"Correct Login");

         [self performSegueWithIdentifier:@"toHome" sender:self];


     } else {
         loginPageStatusLabel.text = @"Incorrect";
         NSLog(@"Login Failed");
     }

 }];

* Assertion failure in -[UIKeyboardTaskQueue waitUntilAllTasksAreFinished], /SourceCache/UIKit_Sim/UIKit-2935.137/Keyboard/UIKeyboardTaskQueue.m:368 2014-05-11 00:06:51.426 LoginTests[3381:3e03] * Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: '-[UIKeyboardTaskQueue waitUntilAllTasksAreFinished]' may only be called from the main thread.'waitUntilAllTasksAreFinished]' may only be called from the main thread.

That is the error being thrown whenever I try to "Log in". The Segue with work if I run it alone, so I am assuming the problem is that the app is trying to go to the next View before it's ready and its causing an error.

I'm fairly new to Obj-C so if I have not posted the adequate information or not called things by the proper names please inform me.

Thank You!

Was it helpful?

Solution

I don't know what value you supplied for queue parameter, but given that your completion block is performing UI updates that must happen on the main thread, you can use [NSOperationQueue mainQueue] (or manually dispatch this code to the main queue). This queue parameter specifies what queue the completion block should be added to, and because you're doing UI related stuff in your completion block, this must be done on the main thread.

Having corrected that, if you are still have assertion errors, you can add an exception breakpoint and that will help confirm precisely where this assertion error is taking place. Or look at your stack trace.

I'd also, in addition to using [NSOperationQueue mainQueue], would suggest doing some more robust error handling:

[NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *error)
 {
     if (!data) {
         // for example, no internet connection or your web server is down

         NSLog(@"request failed: %@", error);
         return;
     }

     if ([response isKindOfClass:[NSHTTPURLResponse class]]) {
         int statusCode = [(NSHTTPURLResponse *)response statusCode];

         if (statusCode != 200) {
             // for example, 404 would mean that your web site said it couldn't find the URL
             // anything besides 200 means that there was some fundamental web server error

             NSLog(@"request resulted in statusCode of %d", statusCode);
             return;
         }
     }

     // if we got here, we know the request was sent and processed by the web server, so now
     // let's see if the login was successful.

     NSString *responseString = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];

     // I'm looking for "Welcome" ... I doubt that's right (but I don't have access to 
     // your web server, so I'm guessing). But the idea is that you have to find whatever
     // appears after successful login that is not in the response if login failed

     if ([responseString rangeOfString:@"Welcome"].location != NSNotFound) {
         loginPageStatusLabel.text = @"Correct";
         NSLog(@"Correct Login");

         [self performSegueWithIdentifier:@"toHome" sender:self];
     } else {
         loginPageStatusLabel.text = @"Incorrect";
         NSLog(@"Login Failed");
     }
 }];
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top