0

我仍在努力,但认为值得一问:

我有一个符合MKAnnotation协议的类,称为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>
)

我自己没有添加任何观察者,所以我覆盖了我AMAnnotation班级的 KVO 方法。事实证明,每次setCoordinate:在将注释添加到地图后调用,我都会看到以下内容:

<AMAnnotation: 0x60800086eac0> _original_setCoordinate:
<AMAnnotation: 0x60800086eac0> addObserver:forKeyPath:options:context: <MKMapAnnotationManager: 0x60800038d270> coordinate

现在,我不知道是什么_original_setCoordinate:,谷歌没有帮助。它看起来像MKMapAnnotationManager重新添加自己作为观察者,但并没有删除自己。

有时当地图被释放时,我看到它会多次删除自己。但其他时候根本没有,这是警告出现的时候。

<AMAnnotation: 0x60800086eac0> removeObserver:forKeyPath: <MKMapAnnotationManager: 0x60800038d270> coordinate
<AMAnnotation: 0x60800086eac0> removeObserver:forKeyPath: <MKMapAnnotationManager: 0x60800038d270> coordinate
<AMAnnotation: 0x60800086eac0> removeObserver:forKeyPath: <MKMapAnnotationManager: 0x60800038d270> coordinate
<AMAnnotation: 0x60800086eac0> removeObserver:forKeyPath: <MKMapAnnotationManager: 0x60800038d270> coordinate
<AMAnnotation: 0x60800086eac0> dealloc

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

4

1 回答 1

1

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

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

于 2014-08-24T22:34:48.250 回答