Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UIAlertView makes the program crash

I've got a crash:

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

And I could not find a solution for 2 days. And here is the code:

[alert dismissWithClickedButtonIndex:0 animated:YES];
UIAlertView *noTicketAlert = [[UIAlertView alloc] initWithTitle:@"Aradığınız kriterlere uygun bilet bulunamadı!" message:nil delegate:self cancelButtonTitle:@"Tamam" otherButtonTitles: nil];
[noTicketAlert show];
like image 561
user2586173 Avatar asked Sep 25 '13 12:09

user2586173


3 Answers

I triggered this error by attempting to display an alert from a background thread. Fixed like this:

dispatch_async(dispatch_get_main_queue(), ^{
    UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:...
    [alertView show];
});
like image 107
Dave Batton Avatar answered Oct 31 '22 10:10

Dave Batton


I got this error when presenting a UIAlertView normally (no funny button override stuff). It turned out that I was presenting it twice in quick succession. The fix in my case was to remove the erroneous duplicate call.

If you do need to present two alert views at close to the same time, and you get this error, then a fix that works (and addresses the error message itself) is to run the code on the main thread:

[[NSOperationQueue mainQueue] addOperationWithBlock:^
    {
    // Your code that presents the alert view(s)
    }];
like image 23
Siegfoult Avatar answered Oct 31 '22 12:10

Siegfoult


Yes, I've found the solution and I share that with you guys. I tried to override the dismissWithClickedButtonIndex function, and sent unique buttonIndexes like 9999 for each of my alerts. That is,

- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex {
    [self viewWillDisappear:YES];
    if(buttonIndex == 9999) {
        noTicketAlert = [[UIAlertView alloc] initWithTitle:@"Aradığınız kriterlere uygun bilet bulunamadı!" message:nil delegate:self cancelButtonTitle:@"Tamam" otherButtonTitles: nil];
        [noTicketAlert show];
    }
}

and if I want to display the noticketAlert, I call this method like :

[alert dismissWithClickedButtonIndex:9999 animated:YES];
like image 1
user2586173 Avatar answered Oct 31 '22 12:10

user2586173