why do I get "wait_fences: failed to receive reply" for this code?
Asked Answered
L

2

2

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
Lucky answered 7/11, 2011 at 5:19 Comment(2)
Check the SO post ... #4242394Isosceles
didn't fix it actually - the wait_fences occurs after the "ModelChangedHandler" is triggeredLucky
W
2

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.

Willywillynilly answered 7/11, 2011 at 7:53 Comment(0)
W
3

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.

Willywillynilly answered 7/11, 2011 at 5:46 Comment(2)
thanks - interestly it didn't seem to fix it....but I see what you mean, and I can the performSelectorOnMainThread getting run first, so your code makes sense. Just that I still get the "wait_fences" line at the end. BY THE WAY your info here kind of points out the response I got here might be wrong #8033152Lucky
uh. I wrote both answers. I don't think they are contradictory. The wait_fences error may be caused by something else by my comment re: threads and UI operations needing to be on the main thread is correct.Willywillynilly
W
2

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.

Willywillynilly answered 7/11, 2011 at 7:53 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.