UIAlertView shown from background thread and with no delegate creates EXC_BAD_ACCESS

旧时模样 提交于 2019-12-21 09:08:31

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!