从后台线程显示的 UIAlertView 并且没有委托创建 EXC_BAD_ACCESS

发布于 2024-12-16 00:08:47 字数 325 浏览 5 评论 0原文

这是我的代码:

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

该代码在后台线程中执行(负责解析),并且错误仅每隔一段时间发生一次。知道这里有什么问题吗?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

甚是思念 2024-12-23 00:08:47

不要从后台线程弄乱 UI。创建一个方法并在主线程上调用该方法:

[someObject performSelectorOnMainThread:@selector(showDebug:)
                             withObject:@"JSON Parsing Error"
                          waitUntilDone:YES];

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];
可爱暴击 2024-12-23 00:08:47

您不应该在单独的线程中执行 UI 代码。

如果您的应用程序有图形用户界面,建议
您接收用户相关事件并启动界面更新
来自应用程序的主线程。这种方法有助于避免
与处理用户事件相关的同步问题和
绘制窗口内容。一些框架,例如Cocoa,通常
需要这种行为,但即使对于那些不这样做的人,也保持这种行为
主线程上的行为具有简化逻辑的优点
用于管理您的用户界面。

线程和您的用户界面

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文