views:

25

answers:

1

The documentation is clear on the fact that animations (invoked with [UIView commitAnimations]) execute on a separate thread. What's not clear (to me, anyway) is whether the animationDidStopSelector is executed on the main (UI) thread or the same thread as the animation.

Given the following code:

- (void) doSomethingCool {
 // Fade out someView
 [UIView beginAnimations:@"myAnimation" context:nil];
 [UIView setAnimationDelegate:self];
 [UIView setAnimationDidStopSelector:@selector(animationDone:finished:context:)];
 someView.alpha = 0.0;
 [UIView commitAnimations];
}

- (void) animationDone:(NSString *)animationID finished:(NSNumber *)finished context:(void *)context {
 if( [animationID isEqual:@"myAnimation"] ) {
  [someView removeFromSuperview];
 }
}

I've heard that accessing UI elements on a thread other than the main thread is "generally bad", so if the animationDone call happens on a different thread, then the above code would do bad things, yes?

It seems not to do bad things. But I have been chasing a seemingly random occasional crash which happens after this animation, and I'm wondering if there's some threading issue.

+1  A: 

It appears that they are executed on the main thread, as proven via a few well-placed NSLog calls.

- (void) doSomethingCool {
    // Fade out someView
    NSLog( @"executing beginAnimations on thread %@", [NSThread currentThread] );
    [UIView beginAnimations:@"myAnimation" context:nil];
    [UIView setAnimationDelegate:self];
    [UIView setAnimationDidStopSelector:@selector(animationDone:finished:context:)];
    someView.alpha = 0.0;
    [UIView commitAnimations];
}

- (void) animationDone:(NSString *)animationID finished:(NSNumber *)finished context:(void *)context {
    NSLog( @"executing animationDone on thread %@", [NSThread currentThread] );
    if( [animationID isEqual:@"myAnimation"] ) {
        [someView removeFromSuperview];
    }
}

… which outputs:

executing beginAnimations on thread <NSThread: 0x6b10860>{name = (null), num = 1}
executing animationDone on thread <NSThread: 0x6b10860>{name = (null), num = 1}

...which makes me wonder where my crash really is. :P

zpasternack