如何在不使用 setCoordinate 的情况下更新注释?
我有一个符合 MKAnnotation 协议的 NSManagedObject 子类,并且它具有用于纬度和经度的 NSNumber 属性。
当我自己更改任何坐标时,我使用 setCooperative: 并更新 setCooperative: 实现中的纬度和经度属性。使用此方法,地图视图会更新注释。但是,当我通过 mergeChangesFromContextDidSaveNotification: 将更改与另一个 NSManagedObjectContext 合并时,不会使用 setCooperative: ,因为纬度和经度属性已显式更改。这可以防止发出任何有关坐标更改的 KVO 通知。
我试图让地图视图意识到坐标取决于此代码的纬度和经度属性:
+ (NSSet *)keyPathsForValuesAffectingValueForKey:(NSString *)key {
NSSet *keyPaths = [super keyPathsForValuesAffectingValueForKey:key];
if ([@"coordinate" isEqualToString:key]) {
NSSet *affectingKeys = [NSSet setWithObjects:@"latitude", @"longitude", nil];
keyPaths = [keyPaths setByAddingObjectsFromSet:affectingKeys];
}
return keyPaths;
}
但是该代码会产生此崩溃:
由于未捕获的异常“NSRangeException”而终止应用程序,原因:“无法删除观察者”对于关键路径“坐标”,因为它没有注册为观察者。'
关于如何防止崩溃或替代方法以在纬度或经度更改时发出坐标 KVO 通知的任何想法?感谢您的关注。
I have a subclassed NSManagedObject that conforms to the MKAnnotation protocol and it has NSNumber properties for latitude and longitude.
When I change any coordinates myself, I use setCoordinate: and update the latitude and longitude properties inside the implementation of setCoordinate:. Using this method, the map view updates the annotations. However, when I merge changes with another NSManagedObjectContext via mergeChangesFromContextDidSaveNotification:, setCoordinate: is not used because the latitude and longitude properties are explicitly changed. This prevents any KVO notifications going out about the coordinate changing.
I have tried to get the map view to realize the coordinate depends on the latitude and longitude properties with this code:
+ (NSSet *)keyPathsForValuesAffectingValueForKey:(NSString *)key {
NSSet *keyPaths = [super keyPathsForValuesAffectingValueForKey:key];
if ([@"coordinate" isEqualToString:key]) {
NSSet *affectingKeys = [NSSet setWithObjects:@"latitude", @"longitude", nil];
keyPaths = [keyPaths setByAddingObjectsFromSet:affectingKeys];
}
return keyPaths;
}
However that code produces this crash:
Terminating app due to uncaught exception 'NSRangeException', reason: 'Cannot remove an observer for the key path "coordinate" from because it is not registered as an observer.'
Any ideas on how to prevent that crash or alternative methods to get the coordinate KVO notification sent out when the latitude or longitude is changed? Thanks for taking a look.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我通过更改
setCooperative:
来修复此问题,以便它使用 原始访问器 由 Core Data 自动生成,用于设置纬度
和经度
属性。我怀疑,因为我的
keyPathsForValuesAffectingValueForKey:
使cooperative
键依赖于latitude
和longitude
键,所以 <当setCooperative:
使用公共访问器而不是原始访问器时,code>MKMapView 会感到困惑。当使用公共访问器方法设置纬度和经度时,任何观察坐标键路径的对象都会观察到三个单独的变化(对于键路径坐标、纬度 和
经度
),而只需要观察一个变化(坐标
),这会混淆地图视图。I fixed this by changing
setCoordinate:
so that it uses the primitive accessors that are automatically generated by Core Data to set thelatitude
andlongitude
properties.I suspect that because my
keyPathsForValuesAffectingValueForKey:
made thecoordinate
key dependent on both thelatitude
andlongitude
keys, theMKMapView
was getting confused whensetCoordinate:
used the public accessors instead of the primitive accessors.When the public accessor methods were used to set the latitude and longitude, any object observing the
coordinate
key path observed three separate changes (for the key pathscoordinate
,latitude
, andlongitude
) when there was only one change (coordinate
) that should be observed, which confused the map view.这有点开销,但您可以观察纬度和经度参数(分别覆盖它们的设置器),并强制设置坐标(使用 setCooperative:< /code>) 当改变时。
It's a little overhead, but you could observe the
latitude
andlongitude
params (resp. override their setters), and force-set the coordinate (withsetCoordinate:
) when the change.