gpt4 book ai didi

macos - MKMapAnnotationManager 未将自身删除为 MKAnnotation 的观察者

转载 作者:行者123 更新时间:2023-12-03 17:51:28 25 4
gpt4 key购买 nike

我仍在研究这个问题,但认为值得一问:

我有一个符合 MKAnnotation 协议(protocol)的类,名为 AMAnnotation。该类还实现了setCooperative:

- (void)setCoordinate:(CLLocationCoordinate2D)newCoordinate
{
NSLog(@"%@ %@", self, NSStringFromSelector(_cmd));
_coordinate = newCoordinate;
}

我可以成功地将注释添加到 map 中,并且可以调用 setCooperative: 并且图钉将在 map 上移动。但有一个问题。 有时,本地图被释放时,程序将在 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 方法。事实证明,每次在将注释添加到 map 后调用 setCooperative: 时,我都会看到以下内容:

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

现在,我不知道 _original_setCooperative: 是什么,Google 没有提供帮助。看起来 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

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

最佳答案

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

您可以使用CLLocationCooperative2DIsValid()检查有效坐标。此外,常量 kCLLocationCooperative2DInvalid 可能会派上用场。

关于macos - MKMapAnnotationManager 未将自身删除为 MKAnnotation 的观察者,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25476653/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com