我的應用在許多線程中通過performSeleactoreOnMain線程方法調用下面的函數:
-(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
試試下面的辦法
-(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];
}