Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UIAlertView shown from background thread and with no delegate creates EXC_BAD_ACCESS

Here is my code:

#ifdef DEBUG
    UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"ERROR" message:@"JSON Parsing Error" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
    [alertView show];
    [alertView release];
#endif

This code is executed in a background thread (responsible for parsing), and the error only happens every other time. Any idea on what is the problem here?

like image 867
Dirty Henry Avatar asked Nov 16 '11 17:11

Dirty Henry


2 Answers

Dont mess with the UI from the background thread. Create a method and call that method on the main thread:

[someObject performSelectorOnMainThread:@selector(showDebug:)
                             withObject:@"JSON Parsing Error"
                          waitUntilDone:YES];
like image 178
Sergio Moura Avatar answered Sep 28 '22 10:09

Sergio Moura


You shouldn't execute UI code in separate thread.

If your application has a graphical user interface, it is recommended that you receive user-related events and initiate interface updates from your application’s main thread. This approach helps avoid synchronization issues associated with handling user events and drawing window content. Some frameworks, such as Cocoa, generally require this behavior, but even for those that do not, keeping this behavior on the main thread has the advantage of simplifying the logic for managing your user interface.

Threads and Your User Interface

like image 36
beryllium Avatar answered Sep 28 '22 08:09

beryllium