I have coded this up for a looping picture viewer on my iPad thinking that this will lead to stackoverflow very soon but nothing so far. It’s steady (not that i am complaining). But is it a case of opening the champagne before the event or…
- (void)showPhotos:(NSMutableArray *)resultSet index:(int)i
{
i += 1;
[UIView animateWithDuration:PHOTO_DISPLAY_TIME
delay:0
options:UIViewAnimationOptionCurveLinear | UIViewAnimationOptionAllowUserInteraction
animations:^(void)
{
//get photourl & set it to imageview
[self.photosImage setAlpha:1.0];
}
completion:^(BOOL finished)
{
if(finished)
{
[UIView animateWithDuration:PHOTO_DISPLAY_TIME
delay:0
options:UIViewAnimationOptionCurveLinear | UIViewAnimationOptionAllowUserInteraction
animations:^(void)
{
[self.photosImage setAlpha:0.0];
}
completion:^(BOOL finished)
{
if(finished)
[self showPhotos:resultSet index:i];
}];
}
}];
return;
}
What I am doing is fading the image in & fading the image out. & then calling the same function again. So this should crash right?
Even if this looks like recursive it is not. The completion block will be called asynchronous after the animation finishes. This will be after your method returns.
If you are still unsure about this you can verify that in the debugger. Just place a breakpoint on your second invocation of your method and examine the stack trace every time it fires.