开发者

MapKit exception: Cannot remove an observer for the key path "title"

开发者 https://www.devze.com 2023-02-12 07:44 出处:网络
I\'m getting quite a lot of crashes like this. In my code, I\'m not doing any KVO on my MKAnnotation objects that could explain this exception. So to me, it looks like an iOS bug.

I'm getting quite a lot of crashes like this. In my code, I'm not doing any KVO on my MKAnnotation objects that could explain this exception. So to me, it looks like an iOS bug.

Did anybody else experience similar crashes?

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

Thread 0 Crashed:
0   libSystem.B.dylib                   0x000792d4 __kill + 8
1   libSystem.B.dylib                   0x000792bd raise + 17
2   WhereTo                             0x000a430d uncaught_exception_handler (PLCrashReporter.m:137)
3   CoreFoundation                      0x000a0adf __handleUncaughtException + 239
4   libobjc.A.dylib                     0x00006593 _objc_terminate + 103
5   libstdc++.6.dylib                   0x00042df9 __cxxabiv1::__terminate(void (*)()) + 53
6   libstdc++.6.dylib                   0x00042e4d std::terminate() + 17
7   libstdc++.6.dylib                   0x00042f1d __cxa_throw + 85
8   libobjc.A.dylib                     0x000054cb objc_exception_throw + 71
9   CoreFoundation                      0x000a07c9 +[NSException raise:format:arguments:] + 69
10  CoreFoundation                      0x000a0803 +[NSException raise:format:] + 35
11  Foundation                          0x00031b4d -[NSObject(NSKeyValueObserverRegistration) _removeObserver:forProperty:] + 545
12  Foundation                          0x000318a1 -[NSObject(NSKeyValueObserverRegistration) removeObserver:forKeyPath:] + 121
13  MapKit                              0x00024e75 -[MKAnnotationContainerView _unregisterObserverForBubbleAnnotation:] + 133
14  MapKit                              0x0001fd29 -[MKAnnotationContainerView setBubbleAnnotationView:] + 73
15  MapKit                              0x0001f4e3 -[MKAnnotationContainerView _showBubbleForAnnotationView:bounce:scrollToFit:userInitiated:avoid:] + 211
16  MapKit                              0x0001f235 -[MKAnnotationContainerView _setSelectedAnnotationView:bounce:pressed:scrollToFit:userInitiated:avoid:] + 357
17  MapKit                              0x0001f0bf -[MKAnnotationContainerView _setSelectedAnnotationView:bounce:pressed:scrollToFit:userInitiated:] + 83
18  MapKit                              0x000248ab -[MKMapView handleT开发者_高级运维ap:] + 195
19  CoreFoundation                      0x0003ebbf -[NSObject(NSObject) performSelector:withObject:] + 23
20  UIKit                               0x0009a3d3 -[UIGestureRecognizer _updateGestureWithEvent:] + 575
21  UIKit                               0x0009a18b -[UIGestureRecognizer _delayedUpdateGesture] + 23
22  UIKit                               0x000017b1 _UIGestureRecognizerUpdateObserver + 437
23  CoreFoundation                      0x00030c59 __CFRUNLOOP_IS_CALLING_OUT_TO_AN_OBSERVER_CALLBACK_FUNCTION__ + 17
24  CoreFoundation                      0x00030acd __CFRunLoopDoObservers + 413
25  CoreFoundation                      0x000280cb __CFRunLoopRun + 855
26  CoreFoundation                      0x00027c87 CFRunLoopRunSpecific + 231
27  CoreFoundation                      0x00027b8f CFRunLoopRunInMode + 59
28  GraphicsServices                    0x000044ab GSEventRunModal + 115
29  GraphicsServices                    0x00004557 GSEventRun + 63
30  UIKit                               0x00037329 -[UIApplication _run] + 413
31  UIKit                               0x00034e93 UIApplicationMain + 671
32  WhereTo                             0x00003475 main (main.m:14)


I had a similar crash with MapKit starting in OS 4.x. By 'similar' I mean it was related to KVO and observers but I don't remember the details.

The problem was: I had a class implenting the MKAnnotation protocol. But in this class I was accessing the coordinate property without using KVO (directly via the corresponding ivar):
_coordinate = ....
instead of
self.coordinate = ...

It seems that Mapkit is now (starting in OS 4) relying on KVO notification to monitor annotations position.


Turns out, I wrote to the property from background threads which is a no-go. If I make sure to only set it from the main thread, the problem seems to go away.


For future reference; I got the exact same error, but in my case it was caused by trying to programmatically select an annotation that hadn't yet been added to the mapview.


I had a similar issue, but the property getter was a defined function but there was no setter. The MKAnnotation protocol requires a property (getter and setter).


I use same in two MKMapView's and exception thrown when call removeAnnotations: from second map, this try-catch fix problem.

@implementation MyAnnotation //<MKAnnotation>
- (void)removeObserver:(NSObject *)observer forKeyPath:(NSString *)keyPath
{
    @try
    {
        [super removeObserver:observer forKeyPath:keyPath];
    }
    @catch(...)
    {

    }
}
@end
0

精彩评论

暂无评论...
验证码 换一张
取 消