我仍在努力,但认为值得一问:
我有一个符合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
有人对此有解释吗?谢谢。