1

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 クラッシュの理由がわかりません: [alert show]

そして、このクラッシュは時々しか起こりません。助けてください。

4

4 に答える 4

2

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];
}
于 2012-12-17T10:41:47.530 に答える
0

代替方法これを使用しない理由:

 [alert performSelectorOnMainThread:@selector(show) withObject:nil waitUntilDone:YES];

それ以外の

 [alert show];
于 2012-12-17T10:58:09.187 に答える
0

問題は、if 条件が true か false かによって、alertview 表示メソッドが機能することです。次のようにメソッドを変更します。

-(void) showAlert: (NSString *)message
{
 if ([NSRunLoop currentRunLoop] != [NSRunLoop mainRunLoop])
 {
    NSLog(@"<< perform in main thread>>");
    [self performSelectorOnMainThread:@selector(showAlert:) withObject:message waitUntilDone:NO];
 }
 else
 {
   UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"Info" message:message delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
  [alert show];
 }
}
于 2012-12-17T10:41:29.393 に答える
0

returnif ブロックの最後にa を置きます。

-(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];
}
于 2012-12-17T10:42:49.640 に答える