MKMapAnnotationManager不会将自身移除为MKAnnotation的观察者

时间:2014-08-24 21:49:18

标签: macos cocoa mapkit key-value-observing kvc

我还在努力解决这个问题,但我觉得值得一提:

我有一个符合名为MKAnnotation的{​​{1}}协议的类。该类还实现了AMAnnotation

setCoordinate:

我可以成功地将注释添加到地图中,然后我可以调用- (void)setCoordinate:(CLLocationCoordinate2D)newCoordinate { NSLog(@"%@ %@", self, NSStringFromSelector(_cmd)); _coordinate = newCoordinate; } ,并且引脚将在地图上移动。但是有一个问题。 有时,当地图被取消分配时,程序将在setCoordinate:暂停,并显示以下消息:

NSKVODeallocateBreak

我自己没有添加任何观察者,因此我在我的An instance 0x608000a626c0 of class AMAnnotation was deallocated while key value observers were still registered with it. Observation info was leaked, and may even become mistakenly attached to some other object. Set a breakpoint on NSKVODeallocateBreak to stop here in the debugger. Here's the current observation info: <NSKeyValueObservationInfo 0x60800045ca70> ( <NSKeyValueObservance 0x600000ac0460: Observer: 0x60800038ead0, Key path: coordinate, Options: <New: NO, Old: YES, Prior: YES> Context: 0x0, Property: 0x60000045fad0> <NSKeyValueObservance 0x600000ac0460: Observer: 0x60800038ead0, Key path: coordinate, Options: <New: NO, Old: YES, Prior: YES> Context: 0x0, Property: 0x60000045fad0> <NSKeyValueObservance 0x600000ac0460: Observer: 0x60800038ead0, Key path: coordinate, Options: <New: NO, Old: YES, Prior: YES> Context: 0x0, Property: 0x60000045fad0> <NSKeyValueObservance 0x600000ac0460: Observer: 0x60800038ead0, Key path: coordinate, Options: <New: NO, Old: YES, Prior: YES> Context: 0x0, Property: 0x60000045fad0> <NSKeyValueObservance 0x600000ac0460: Observer: 0x60800038ead0, Key path: coordinate, Options: <New: NO, Old: YES, Prior: YES> Context: 0x0, Property: 0x60000045fad0> <NSKeyValueObservance 0x600000ac0460: Observer: 0x60800038ead0, Key path: coordinate, Options: <New: NO, Old: YES, Prior: YES> Context: 0x0, Property: 0x60000045fad0> <NSKeyValueObservance 0x600000ac0460: Observer: 0x60800038ead0, Key path: coordinate, Options: <New: NO, Old: YES, Prior: YES> Context: 0x0, Property: 0x60000045fad0> <NSKeyValueObservance 0x600000ac0460: Observer: 0x60800038ead0, Key path: coordinate, Options: <New: NO, Old: YES, Prior: YES> Context: 0x0, Property: 0x60000045fad0> ) 课程上覆盖了KVO方法。事实证明,每次在注释添加到地图后调用AMAnnotation时,我都会看到以下内容:

setCoordinate:

现在,我不知道<AMAnnotation: 0x60800086eac0> _original_setCoordinate: <AMAnnotation: 0x60800086eac0> addObserver:forKeyPath:options:context: <MKMapAnnotationManager: 0x60800038d270> coordinate 是什么,Google没有帮助。看起来_original_setCoordinate:看起来像是一个观察者而不是自我移除。

有时当地图被解除分配时,我发现它会多次删除自己。但其他时间根本没有,这就是警告出现的时候。

MKMapAnnotationManager

有人对此有解释吗?感谢。

1 个答案:

答案 0 :(得分:1)

我讨厌这种情况发生,但看起来我解决了它。在使用无效坐标更新注释时,MKMapAnnotationManager似乎感到困惑。 (至少,这是我在调试此问题时所观察到的。)如果我在更新注释之前检查无效坐标,如果注释无效则删除注释并在有效时重新添加,似乎根据需要工作并正确调用add / remove观察者方法。

您可以使用CLLocationCoordinate2DIsValid()检查有效坐标。常量kCLLocationCoordinate2DInvalid也可以派上用场。