why do I get “wait_fences: failed to receive reply” for this code?

China☆狼群 提交于 2019-12-30 11:27:07

问题


why do I get "wait_fences: failed to receive reply" for this code? Is it the way I'm using notification to communicate back to the main thread?

#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

Output is:

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

回答1:


Here's how to get rid of the wait_fences error. Change the line where you dismiss the alertView to use animation as follows:

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

I think wait_fences has something to do with view animation states with alert views but it's hard to know for sure. I do think this should eliminate the error msg though. My other answer doesn't directly get rid of the error but I still recommend it. UI actions should be done on the main thread.




回答2:


There is an obvious problem here. You are posting the notification from the background thread (which is fine) which means the notification handler ModelChangedHandler is being called on the background thread. The handler is then dismissing an alert view which must be done on the main thread. Try changing your code to:

- (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];
    }
}

EDIT: was typing too fast, changed answer to reflect the correct UI objects.



来源:https://stackoverflow.com/questions/8032987/why-do-i-get-wait-fences-failed-to-receive-reply-for-this-code

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!