Sunday, February 16, 2014

UIAlertView

How many of you have faced this problem in iOS programming:

Assertion failure in -[UIKeyboardTaskQueue performTask:], /SourceCache/UIKit/UIKit-2903.23/Keyboard/UIKeyboardTaskQueue.m:388
Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: '-[UIKeyboardTaskQueue performTask:] may only be called from the main thread.'
Recently, I faced this problem while trying to popup a UiAlertView after a success/failure of method sendAsynchronousRequest of Object NSURLConnection as described in THIS ARTICLE

After the problem, I concluded that the show method of UiAlertView has to be called from the main thread only.

So far as my understanding, after the success or failure of sendAsynchronousRequest method, the delegated methods are run in new thread. This is the reason for the exception I got.
Fortunately I found working solutions for this and I realized that there are more than one solution.

Solution 1


[alertView performSelectorOnMainThread:@selector(show) withObject:nil waitUntilDone:YES];

Solution 2


if(isSuceess){
    UIAlertView *alertview = [[UIAlertView alloc] initWithTitle:@"Event" message:@"Event added in calendar" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
    dispatch_async(dispatch_get_main_queue(), ^{
        [alertview show];
    });
}
else {
    UIAlertView *alertview = [[UIAlertView alloc] initWithTitle:@"Event" message:[err description] delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
    dispatch_async(dispatch_get_main_queue(), ^{
       [alertview show];
    });
}








No comments:

Post a Comment