Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MKMapView - Removing annotation causes app crash

Removing annotations from my map view in the following way:

 if ([[self.mapView annotations] count] > 0)
{
    [self.mapView removeAnnotations:[self.mapView annotations]];
}

causes my application to crash with the following exception:

*** Terminating app due to uncaught exception 'NSRangeException', reason: 'Cannot remove an observer <MKAnnotationContainerView 0xe87b420> for the key path "title" from <PFAnnotation 0x10851230> because it is not registered as an observer.'

The annotations are added in the following way:

 CLLocationCoordinate2D pinPosition;
for (int index = 0; index < [array count]; index++)
{        
    Station *aStation = [array objectAtIndex:index];
    PFAnnotation *stationPin = [[PFAnnotation alloc] init]; //StationPinView
    pinPosition = CLLocationCoordinate2DMake([[aStation valueForKey:@"latitude"] doubleValue], [[aStation valueForKey:@"longitude"] doubleValue]);
    stationPin.stationName = [aStation valueForKey:@"stationName"];
    stationPin.stationPosition = pinPosition;
    stationPin.stationLength = [aStation valueForKey:@"platformLength"];

    [self.mapView addAnnotation:stationPin];
    [stationPin release];        


}

My PFAnnotation.h is:

@interface PFAnnotation : NSObject <MKAnnotation>
{
    NSString *stationName;
    CLLocationCoordinate2D stationPosition;
    NSNumber *stationLength;

}

@property (nonatomic, retain) NSString *stationName;
@property CLLocationCoordinate2D stationPosition;
@property (nonatomic, retain) NSNumber *stationLength;


@end

and my PFAnnotation.m is:

@implementation PFAnnotation

@synthesize stationName;
@synthesize stationPosition;
@synthesize stationLength;


- (CLLocationCoordinate2D)coordinate;
{
    return stationPosition; 
}

- (NSString *)title
{
    return stationName;

}

- (NSString *)subtitle
{
    if (stationLength == nil)
        return nil;
    else
        return [NSString stringWithFormat:@"Platform Length: %@ft",stationLength];
}


- (void)dealloc {
    [stationName release];
    [stationLength release];
    [super dealloc];
}

I have read in some other threads that, setting the annotation properties from background thread is the reason for the above error. But in my case,it is not so because every thing is performed on the main thread. Please advise.

like image 489
DroidHeaven Avatar asked Jun 13 '12 09:06

DroidHeaven


1 Answers

ok..solved it at last!!! I think it was due to the animation provided during addition of annotations. since there were a number of annotations that were added back-to-back with animation and also the annotations were removed just before the animation started, there could have been a reference to the released annotation(this is my guess). Moreover, the removal+addition process was made on each regionDidChangeAnimated call, which could have made a overlap between the removal and addition process. Anyway, how I solved it was that, I provided a timer which will be fired only after 1 second after every regionDidchangeAnimated to make sure that the user has done with dragging. Thus unnecessary addition+removal of annotations was avoided and I was able to avoid the crash. Thanks to all guys here for their time taken to support me, especially Guntis Treulands.

like image 185
DroidHeaven Avatar answered Sep 21 '22 00:09

DroidHeaven