I have the following code snippet that causes a crash with the following stack trace. How I reproduce this crash is to navigate out of the view controller, which has this code, while it's still loading as map annotations are added as a result of a network request.
I have narrowed it down the cause to selectAnnotation call with the animated flag set to YES. If I change that flag to NO, then the crash no longer happens.
Does anyone have any clues that explain this behavior. Would love to know what is going on.
Thanks in advance!
- (void)mapView:(MKMapView *)mapView didAddAnnotationViews:(NSArray *)annotationViews 
{  
    __weak typeof(self) weakSelf = self;
    for (MKAnnotationView *annotationView in annotationViews) {
        CGRect endFrame = annotationView.frame;
        annotationView.frame = CGRectOffset(endFrame, 0, -CGRectGetHeight(self.mapView.frame));
        void (^ animationBlock)() = ^{
            annotationView.frame = endFrame;
        };
        void (^ completionBlock)(BOOL) = ^(BOOL finished){
            [self.mapView selectAnnotation:self.annotation animated:YES];
        };
        [UIView animateWithDuration:0.5
                         animations:animationBlock
                         completion:completionBlock];
    }
}
Stack trace:
Thread 0 Crashed:
1   libobjc.A.dylib objc_msgSend + 16
2   UIKit   __125-[UIPopoverController _presentPopoverFromRect:embeddedInView:usingViewForLayoutConstraints:permittedArrowDirections:animate:]_block_invoke421 + 92
3   UIKit   -[UIViewAnimationBlockDelegate _didEndBlockAnimation:finished:context:] + 404
4   UIKit   -[UIViewAnimationState sendDelegateAnimationDidStop:finished:] + 184
5   UIKit   -[UIViewAnimationState animationDidStop:finished:] + 100
6   QuartzCore  CA::Layer::run_animation_callbacks(void*) + 292
7   libdispatch.dylib   _dispatch_client_callout + 12
8   libdispatch.dylib   _dispatch_main_queue_callback_4CF + 928
9   CoreFoundation  __CFRUNLOOP_IS_SERVICING_THE_MAIN_DISPATCH_QUEUE__ + 8
10  CoreFoundation  __CFRunLoopRun + 1488
11  CoreFoundation  CFRunLoopRunSpecific + 392
12  GraphicsServices    GSEventRunModal + 164
13  UIKit   UIApplicationMain + 1484
14  main (main.m:15)
15  libdyld.dylib   start + 
 
     
    