93

I get this cryptic error the first time (and only the first time) my view is loaded due to the following line of code:

- (void)viewWillAppear:(BOOL)animated
{
    [textField becomeFirstResponder];
}

There is a noticeable (~3 – 4 second, even on the simulator) delay due to this that makes my app feel unresponsive. Does anyone know how to fix this? I can't find any documentation on it on Apple's site, or any solutions here or on Google.

Strangely, the opposite situation happens if I put the line in -viewDidAppear: instead of -viewWillAppear:; that is, instead of printing the error only the first time the keyboard is shown and never again, the error is not printed the first time but every time after. This is causing a major headache for me.

Michael
  • 4,700
  • 9
  • 35
  • 42

18 Answers18

102

Override -viewDidAppear:, not -viewWillAppear, and make sure to call [super viewDidAppear:]. You should not perform animations when you are not on screen ("will appear"). And the -viewDidAppear: docs explain that you must call super because they have their own things to do.

Rob Napier
  • 286,113
  • 34
  • 456
  • 610
  • This is not as responsive as I'd like (there's still a slight delay showing the keyboard each time), but it seems to do the trick. – Michael Sep 11 '09 at 18:27
  • 1
    I know the lag you're talking about. I've seen it in a lot of apps. You might try calling -becomeFirstResponder before (rather than after) calling -[super viewDidAppear:] if you're not already. This may have no impact, but might get the animation started in the same event loop rather than the next one. I haven't experimented with this yet to confirm. – Rob Napier Sep 14 '09 at 13:22
  • 14
    This doesn't fix the problem. If you throw up a UIAlertSheet in viewDidAppear, after calling [super viewDidAppear:animated], you end up with the same message, every time. if, however, you throw it up afterwards, say in response to an ibaction, no problem. so performWithSelector is probably the way to fix, or you could ignore the message, either way this appears to be an SDK bug, not a problem with your code. – Billy Gray Jun 15 '10 at 20:36
  • 9
    @Billy, throwing up a UIAlertSheet before animations are done would likely cause the same problem. In any case you're putting up a sheet inside of viewDidAppear is probably too early and you probably should use performSelector:afterDelay: to push the UIAlertSheet to the next loop. That's not a bug in the SDK, though the details here are poorly documented. Performing animations in -viewWillAppear was the bug in the original code. In either case, you shouldn't ignore the message. It can lead to odd visual artifacts (a strange sideways sliding of the animation). – Rob Napier Jun 20 '10 at 15:37
  • 4
    @Michael, I've provided a [solution for `becomeFirstResponder` to show the keyboard immediately, without delay](http://stackoverflow.com/questions/1371346/wait-fences-failed-to-receive-reply-10004003/6880606#6880606). – ma11hew28 Jul 30 '11 at 01:58
  • Billy and Rob's comments solved my issue with a UIAlertView. performSelect:afterDelay fixes it! – Matt Connolly Oct 14 '11 at 04:16
  • @RobNapier if you found this "wait_fences: failed to receive reply" and had NO idea what to attribute it to (definitely not `viewWillAppear`, though) where might you begin searching for the problem? – Dan Rosenstark May 07 '12 at 15:19
  • Look at your animations. Make sure you don't run them while off screen. Look for animating things that you just created (and so aren't onscreen yet). Make sure that all animation starts match with animation ends (for view animations that have being and end calls). Don't implicitly do things that animate while offscreen (like becomeFirstResponder). Watch for animation completion blocks that start new animations while offscreen. Look for presenting a view controller in the middle of another animation. Most of all, look for anywhere you are being "clever" with your animations and be simpler. – Rob Napier May 07 '12 at 15:55
22

I was getting a similar error when quickly:

  1. Dismissing a modal view
  2. Updating the main view
  3. Presenting a new modal view

I noticed I was only getting it in the simulator and not on the device. Additionally, I was getting caught in an infinite loop.

My solution was to delay the presenting of the new modal view. It seems that quickly updating the view hierarchy caused some sot of race condition in Apple's code.

With that in mind, try this:

     - (void)viewDidAppear:(BOOL)animated{

            [super viewDidAppear:animated];
            [textField performSelector:@selector(becomeFirstResponder) withObject:nil afterDelay:0.1];
  }

You may be having issues presenting the keyboard for a UITextField that ins't yet on screen. This may be causing problems similar to mine.

Also, you pause giving the hierarchy time to update before presenting the keyboard, just in case.

Hope this helps.

Corey Floyd
  • 25,929
  • 31
  • 126
  • 154
  • 3
    To the issues you were having with modals, did you wait for the modal to finish dismissing before presenting the new modal? Rather than waiting an arbitrary period of time and hoping it's completed, you can know by overriding -viewDidDisappear in the modalViewController. That can call back to the modal's -parentViewController, or could post a notification. The key is to understand that asking something to dismiss does not mean it is gone yet, and you shouldn't animate things on top of each other in general. -viewWill/DidDisappear is generally your best way to know for sure when things happen. – Rob Napier Sep 11 '09 at 14:22
  • The first modal view was the photo picker, and I handles everything within the photo picker call back method. Your right, I should have place the code to launch the next modal view within viewDdiAppear. that is a better solution and would most likely fix the issue regardless of platform. – Corey Floyd Sep 11 '09 at 14:41
12

Check you are only interacting with the UI on the main thread. I got wait_fences: failed to receive reply: 10004003 while I was sitting there waiting for a UIAlertView to show for about 5 seconds because the relevant code was executed on a background thread. You can make sure by putting your code in block and sending it to the main thread:

dispatch_async(dispatch_get_main_queue(), ^{
    if (!success) {
        // Inform user that import failed
        UIAlertView * importFailedAlert = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"ErrorTitle5", @"Import failed") 
                                                                     message:NSLocalizedString(@"Error5", @"Something went wrong") 
                                                                    delegate:nil 
                                                           cancelButtonTitle:NSLocalizedString(@"OK", nil) 
                                                           otherButtonTitles:nil];
        [importFailedAlert show];
    }
});
diachedelic
  • 2,195
  • 1
  • 24
  • 28
9

After trying everything I could find on Google and none of it working, this is what solved the problem for me. The key is that I'm doing this stuff in the willDismissWithButtonIndex delegate method. Before I was doing it elsewhere.

- (void)alertView:(UIAlertView *)alertView willDismissWithButtonIndex:(NSInteger)buttonIndex
{
    [myTextField resignFirstResponder];
    [myTextField removeFromSuperview];  
    [myTextField release];  
}
warehouselabs
  • 91
  • 1
  • 1
8

If you have the following line in viewDidLoad, it can cause this message. Comment the following line.

[[UIApplication sharedApplication] setStatusBarHidden:YES]; //This line should be commented

(You can disable the status bar from the application plist file instead).

Hemang
  • 26,840
  • 19
  • 119
  • 186
rlcoder
  • 281
  • 3
  • 3
7

After few tests the big rule is: "Do not perform animation before animated dismissal or animated show.".

For example:

  • do not call -dismissModalViewControllerAnimated:YES after the delegation callback of an UIAlertView -alertView:willDismissWithButtonIndex: (wait the fade out of the alert view before doing this using the -alertView:didDismissWithButtonIndex: callback)
  • do not try to show the keyboard (becomeFirstResponder) before your view controller is on screen.

Bad things may happen.

Hope it will be useful ;-)

Jason Plank
  • 2,336
  • 5
  • 31
  • 40
nverinaud
  • 1,270
  • 14
  • 25
  • I was using clickedButtonAtIndex, and would do a bunch of textfield populating before the alertview was dismissed. Switching it to didDismissWithButtonIndex certainly helped getting rid of those warnings! Thanks! – Nitin Alabur Apr 27 '12 at 14:28
5

You have done [textfield becomeFirstResponder];

And after you get the value from textfield in your code, do [textfield resignFirstResponder];. That will help you, I think.

Jason Plank
  • 2,336
  • 5
  • 31
  • 40
Gani
  • 709
  • 3
  • 13
  • 21
5

This worked for me to get the keyboard to show itself immediately, without animation or delay.

Let textField be an instance variable of MyViewController (a subclass of UIViewController).

Call [textField becomeFirstResponder] in initWithNibName:bundle: (for a subclass of UIViewController) or initWithStyle: (for a subclass of UITableViewController), not in viewDidLoad. E.g.:

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        [textField becomeFirstResponder];
    }
    return self;
}

Or, call it just after initializing but before pushing the UIViewController. E.g.:

MyViewController *viewController = [[MyViewController alloc] init];
[viewController.textField becomeFirstResponder];
[self.navigationController pushViewController:viewController animated:YES];
[viewController release];
ma11hew28
  • 121,420
  • 116
  • 450
  • 651
  • Interesting. Are you sure this works all the time? My concern is that you don't reference `view`, so it's not certain that the nib file has been loaded. If `textField` is an IBOutlet, then I would think it would be nil at this point. – Rob Napier Aug 15 '11 at 21:43
4

If you're running the current iPhone Simulator 4.0, this error message appears frequently when rotating the screen (or when animating after rotating the screen) accompanied by 1-2 second lag in the animations.

It is a bug in this version of the Simulator and should be fixed soon.

Matt Gallagher
  • 14,858
  • 2
  • 41
  • 43
  • thanks for info about iOS4 simulator bug. for the same project, the `wait_fences` message didn't show up in the 3.1 simulator – ohho Jun 09 '10 at 08:49
3

I can simulate this one-on-one by means of this UIAlertView code.

   UIAlertView *alert = [[UIAlertView alloc]
                   initWithTitle:NSLocalizedString(@"defineTitle",@"defineTitle")
                         message:NSLocalizedString(@"defineBody", @"defineBody")
                        delegate:self
               cancelButtonTitle:NSLocalizedString(@"Ok", @"Ok")
               otherButtonTitles:nil];
   [alert show];

When the NSLocalizedString are not defined in the Localizable.strings file it will take to long to search for the texts, so the alert will show and the “wait_fences: failed to receive reply: 10004003” will be shown.

For me I only had to add the texts to the Localizable.strings files and my problems were solved. Maybe this is also the case for other occurances?

Vincent
  • 4,342
  • 1
  • 38
  • 37
3

See here for more info: http://www.iphonedevsdk.com/forum/iphone-sdk-development-advanced-discussion/17373-wait_fences-failed-receive-reply-10004003-a.html

Your problem is related.

Andrew Johnson
  • 13,108
  • 13
  • 75
  • 116
  • Thanks for the help, but unfortunately I can't find a solution for my problem on that page. "It seems to occur when a subview (eg. UIAlertView) is created prior to its parent/super view." This shouldn't be happening in the above code, right? – Michael Sep 05 '09 at 08:47
3

override viewDidappear, not viewWillAppear:

-(void) viewDidAppear:(BOOL) animated
{
 [super viewDidAppear:animated];
 [myTextField becomeFirstResponder];
}
Jason Plank
  • 2,336
  • 5
  • 31
  • 40
Wagh
  • 2,678
  • 1
  • 25
  • 29
1

The problems is that there's a race condition in Apple's code. Usually, this has to something to do with incorrect UI updates.

In my experience, you either haven't called the super in viewDidAppear, viewWillAppear etc. Or you try to display a UIAlertView in viewDidLoad or viewWillAppear.

When you add a UIAlertView, the framework needs a reference to your parent view. But if you're in viewWillAppear or viewDidLoad, the view isn't actually displayed... You should consider move the code to viewDidAppear where the view is ready to be used by UIAlertView.

ABCD
  • 7,914
  • 9
  • 54
  • 90
1

Also with the UIAlertView. What solved it for me was having the resign as below, as warehouselabs mentioned earlier.

- (void)didPresentAlertView:(UIAlertView *)alertView
{
    [txtListingPassword becomeFirstResponder];
}

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    [txtListingPassword resignFirstResponder];
}

The other delegates of UIAlertViewDelegate did not fix the issue.

Marcus
  • 11
  • 1
0

Alertview or actionsheets should be shown on main threads...so if your making any synchronous connections and performing that operation on another thread and showin alerts on the basis of output you received from that operation then you will get this error message wait_fences: failed to receive reply: 10004003 . You can do something like....

[self performSelectotOnMainThread:@selector(handleOutput:) withObject:output waitUntilDone:YES/NO];

and show alerts in handleOutput method passing the output response string as the parameter.

j0k
  • 22,600
  • 28
  • 79
  • 90
Vishal Singh
  • 4,400
  • 4
  • 27
  • 43
0

Solution is here!

I had same error, now I got the solution, this may help you.

- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex{
  [self performSelector:@selector(YOUR_METHOD) withObject:nil afterDelay:0.1];
}
soumya
  • 3,801
  • 9
  • 35
  • 69
SachinVsSachin
  • 6,401
  • 3
  • 33
  • 39
0

Is the Text Field contained within that view, or within something else? You can only send the 'becomeFirstRepsonder' to something that is contained directly within that view. If it's stored in some other widget component, you shouldn't set the first responder status in this widget, but rather in the widget that's being created. For example, if you're adding the text field to an alert view, because the show happens asynchronously, it might not be up by the time you call the becomeFirstResponder. (Ideally, you'd have your own alert view class and define the text field within that, and when that view receives the viewDidAppear, you'd set the textfield as first responder at that point.)

AlBlue
  • 23,254
  • 14
  • 71
  • 91
0

I also get the message wait_fences: failed to receive reply: 10004003 and my viewWill... and viewDid... methods do nothing but send messages to super. In my case it happens when I have a UIAlertView showing in my GameViewController and the user instead presses the iPhone round device button and then returns to the app. This looks out of my hands.

Ken
  • 30,811
  • 34
  • 116
  • 155