问题
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?
回答1:
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];
回答2:
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
来源:https://stackoverflow.com/questions/8155735/uialertview-shown-from-background-thread-and-with-no-delegate-creates-exc-bad-ac