1

I have to move some code from viewDidAppear to a new selector to fire after 0.1f seconds. So I have done something like:

-(void) viewDidAppear:(BOOL)animated{
    [self performSelector:@selector(startShowing) withObject:nil afterDelay:0.1f];      
}

-(void) startShowing{
    NSLog(@"start Showing");
    ............
}

When I start the application, nothing happens, "start Showing" is not appearing. If I change the view and then come back to the first one, it is working.

Anybody know why?

CristiC
  • 22,068
  • 12
  • 57
  • 89

1 Answers1

2

The problem is that the delay relies on the underlying implementation of NSTimer, which relies on an NSRunLoop, which is instantiated by the time the application finishes launching.

From the Threading Programming Guide:

"When performing a selector on [a] thread, the target thread must have an active run loop. For threads you create, this means waiting until your code explicitly starts the run loop. Because the main thread starts its own run loop, however, you can begin issuing calls on that thread as soon as the application calls the applicationDidFinishLaunching: method of the application delegate. The run loop processes all queued perform selector calls each time through the loop, rather than processing one during each loop iteration."

So check and see if applicationDidFinishLaunching: has fired yet. You said you had just started up the application. I bet it's not done launching yet.

If that's the problem you could fix it a few ways. The simplest to me is to call performSelector:withObject:afterDelay: from applicationDidFinishLaunching.

AlleyGator
  • 1,266
  • 9
  • 13
  • Thanks. Seems fair. Will try it tonight. Any other way I can fix it, if this will be the case? – CristiC Oct 18 '11 at 11:22
  • Other ways? Well... Apple has semi-deprecated applicationDidFinishLaunching: in favor of the application:didFinishLaunchingWithOptions: method also used by the UIApplicationDelegate class. You should probably use application:didFinishLaunchingWithOptions:. Your solution really depends on why you're calling startShowing in this way. Are you trying to work around another timing issue? Key Value Observing would be useful if you want to be notified when the application delegate knows it has been launched. – AlleyGator Oct 18 '11 at 20:40