2011-11-07 29 views
2

为什么我得到这个代码的“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 
+0

检查SO帖子... http://stackoverflow.com/questions/4241894/wait-fences-failed-to-receive-reply-10004003-error-uialert-without-uitextfie – Jhaliya

+0

没有修复它实际上 - 在触发“ModelChangedHandler”后发生了wait_fences – Greg

回答

2

下面是如何摆脱wait_fences错误。更改,您驳回alertView如下使用动画线:

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

我觉得wait_fences事做,可欣赏动画状态与警惕的观点,但很难知道。我认为这应该消除错误味精虽然。我的其他答案不直接摆脱错误,但我仍然建议。 UI操作应该在主线程上完成。

+0

是的 - 就是这样! – Greg

3

这里有一个明显的问题。你从后台线程发布通知(这很好),这意味着在后台线程上调用通知处理程序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对象。

+0

感谢 - 有趣的是它似乎没有解决它......但我明白你的意思,我可以先运行performSelectorOnMainThread,这样你的代码才有意义。只是我仍然在最后得到了“wait_fences”这一行。由此你的信息在这里有点指出我在这里得到的回应可能是错误的http://stackoverflow.com/questions/8032652/is-it-ok-to-use-of-a-notification-to-communication-回到主线程的 – Greg

+0

嗯。我写了两个答案。我不认为他们是矛盾的。 wait_fences错误可能是由我的评论引起的:re:需要在主线程中的线程和UI操作是正确的。 – XJones

相关问题