MKMapView - 删除注释会导致应用程序崩溃

Dro*_*ven 10 iphone xcode cocoa-touch ios ios5

通过以下方式从地图视图中删除注释:

 if ([[self.mapView annotations] count] > 0)
{
    [self.mapView removeAnnotations:[self.mapView annotations]];
}
Run Code Online (Sandbox Code Playgroud)

导致我的应用程序崩溃,出现以下异常:

*** 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.'
Run Code Online (Sandbox Code Playgroud)

注释按以下方式添加:

 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];        


}
Run Code Online (Sandbox Code Playgroud)

我的PFAnnotation.h是:

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

}

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


@end
Run Code Online (Sandbox Code Playgroud)

和我的PFAnnotation.m是:

@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];
}
Run Code Online (Sandbox Code Playgroud)

我在其他一些主题中已经读过,从后台线程设置注释属性是导致上述错误的原因.但就我而言,并非如此,因为每一件事都是在主线程上执行的.请指教.

Dro*_*ven 5

好的...终于解决了!我认为这是由于添加注释时提供的动画.由于有许多注释与动画背靠背添加,并且注释在动画开始之前被删除,因此可能存在对已发布注释的引用(这是我的猜测).此外,删除+添加过程是在每个regionDidChangeAnimated调用上进行的,这可能会在删除和添加过程之间产生重叠.无论如何,我如何解决这个问题,我提供了一个计时器,它将在每个regionDidchangeAnimated之后仅在1秒后触发,以确保用户完成了拖动操作.因此避免了不必要的添加+删除注释,并且我能够避免崩溃.感谢所有人在这里支持我的时间,尤其是Guntis Treulands.