为什么我得到这个代码的“wait_fences:未能收到答复”?

为什么我得到这个代码的“wait_fences:未能收到答复”? 这是我使用通知回传给主线程的方式吗?

#import "ViewController.h" @implementation ViewController @synthesize alert; #pragma mark - Background Thread Test Methods - (void) ConfigTasksForBackground:(id)sender{ NSLog(@"ConfigTasksForBackground - Starting"); [NSThread sleepForTimeInterval:6]; [[NSNotificationCenter defaultCenter] postNotificationName:@"ModelChanged" object:self]; NSLog(@"ConfigTasksForBackground - Ending"); } #pragma mark - Callbacks - (void) ModelChangedHandler:(NSNotification *) notification { if ([[notification name] isEqualToString:@"ModelChanged"]) { NSLog(@"ModelChangedHandler"); [self.alert dismissWithClickedButtonIndex:0 animated:false]; } } #pragma mark - View lifecycle - (void)viewDidLoad { [super viewDidLoad]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(ModelChangedHandler:) name:@"ModelChanged" object:nil]; } - (void)viewDidAppear:(BOOL)animated { [super viewDidAppear:animated]; self.alert = [[[UIAlertView alloc] initWithTitle:@"Title" message:@"viewDidAppear" delegate:nil cancelButtonTitle:nil otherButtonTitles:nil] autorelease]; [alert show]; [self performSelectorInBackground:@selector(ConfigTasksForBackground:) withObject:nil]; } @end 

输出是:

 2011-11-07 15:15:42.730 test_background[6876:13603] ConfigTasksForBackground - Starting 2011-11-07 15:15:48.734 test_background[6876:13603] ModelChangedHandler 2011-11-07 15:15:49.236 test_background[6876:13603] ConfigTasksForBackground - Ending wait_fences: failed to receive reply: 10004003 

以下是如何摆脱wait_fences错误。 将您closuresalertView的行更改为使用animation,如下所示:

 [self.alert dismissWithClickedButtonIndex:0 animated:YES]; 

我认为wait_fences与查看具有警报视图的animation状态有关,但是很难确定。 我认为这应该消除错误味精虽然。 我的其他答案不直接摆脱错误,但我仍然build议。 UI操作应该在主线程上完成。

这里有一个明显的问题。 你从后台线程发布通知(这很好),这意味着在后台线程上调用通知处理程序ModelChangedHandler 。 处理程序然后解除必须在主线程上完成的警报视图。 尝试将您的代码更改为:

 - (void) ModelChangedHandler:(NSNotification *) notification { if (![NSThread isMainThread]) { [self performSelectorOnMainThread:@selector(ModelChangedHandler:) withObject:notification waitUntilDone:NO]; } else if ([[notification name] isEqualToString:@"ModelChanged"]) { NSLog(@"ModelChangedHandler"); [self.alert dismissWithClickedButtonIndex:0 animated:false]; } } 

编辑:键入得太快了,改变了答案,以反映正确的UI对象。