Mick F
Mick F

Reputation: 7439

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?

Upvotes: 9

Views: 5393

Answers (2)

beryllium
beryllium

Reputation: 29767

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

Upvotes: 2

Sergio Moura
Sergio Moura

Reputation: 4942

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];

Upvotes: 17

Related Questions