1

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 + 
Adis
  • 4,512
  • 2
  • 33
  • 40
dfujiwara
  • 353
  • 2
  • 11
  • Unrelated but: Calling selectAnnotation on all the annotations in a loop is a bit pointless since only one annotation at a time can be "selected". Only the last annotation's select will stick. –  Oct 19 '14 at 19:02

1 Answers1

1

You pretty much figured out the issue there, it's selecting the annotation with animation, while the controller showing the callout is not the topmost controller.

I suggest you animate the callouts only if the controller's view is visible, and don't animate them only if it is, check this question out for a handy method for visibility checking: How to tell if UIViewController's view is visible

Community
  • 1
  • 1
Adis
  • 4,512
  • 2
  • 33
  • 40