gpt4 book ai didi

iphone - Exc_Bad_Access 有时仅在显示警报 View 时

转载 作者:行者123 更新时间:2023-11-29 11:05:17 28 4
gpt4 key购买 nike

我有很多线程通过 performSeleactoreOnMainThread 方法调用以下函数:

-(void) showAlert: (NSString *)message{
if ([NSRunLoop currentRunLoop] != [NSRunLoop mainRunLoop]) {
NSLog(@"<< perform in main thread>>");
[self performSelectorOnMainThread:@selector(showAlert:) withObject:message waitUntilDone:NO];
}
UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"Info" message:message delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
[alert show];
}

因为,在任何情况下,这个方法只会在主线程上调用,我没有得到 EXC_BAD_ACCESS 崩溃的原因: [警报显示]

而且这种崩溃只是偶尔发生。请帮忙。

最佳答案

我猜你忘了在你的代码中添加 return; 这样你的 if 下面的代码也会被执行,无论它是否在主循环中。

一个简单的修复可能是:

-(void) showAlert: (NSString *)message{
if ([NSRunLoop currentRunLoop] != [NSRunLoop mainRunLoop]) {
NSLog(@"<< perform in main thread>>");
[self performSelectorOnMainThread:@selector(showAlert:) withObject:message waitUntilDone:NO];
return;
}
UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"Info" message:message delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
[alert show];
}

关于iphone - Exc_Bad_Access 有时仅在显示警报 View 时,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13912505/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com