我给你写信是因为我遇到了一个非常奇怪的错误。我试图在我的应用程序上添加 GMS,最后,当我必须获取设备位置时,我遇到了这个崩溃:
由于未捕获的异常“NSInternalInconsistencyException”而终止应用程序,原因:“GMSMapView 类的实例 0x7ff7fd82def0 已被释放,而键值观察者仍向其注册。当前观察信息:(上下文:0x0,属性:0x7ff7fdc2f470>
代码在这里初始化:
self.mapView.myLocationEnabled = YES;
self.mapView.mapType = kGMSTypeNormal;
self.mapView.settings.compassButton = YES;
self.mapView.settings.myLocationButton = YES;
//=> Listen to the myLocation property of GMSMapView.
[self.mapView addObserver:self
forKeyPath:@"myLocation"
options:NSKeyValueObservingOptionNew
context:NULL];
- (void)observeValueForKeyPath:(NSString *)keyPath
ofObject:(id)object
change:(NSDictionary *)change
context:(void *)context
{
if ([keyPath isEqualToString:@"myLocation"] && [object isKindOfClass:[GMSMapView class]])
{
appDelegate().curUserLocation = [change objectForKey:NSKeyValueChangeNewKey];
[self.mapView animateToCameraPosition:[GMSCameraPosition cameraWithLatitude:self.mapView.myLocation.coordinate.latitude
longitude:self.mapView.myLocation.coordinate.longitude
zoom:15]];
}
}
仍然没有成功。我在observerValueForKey方法中设置了断点,一旦从这里出来,就会出现崩溃。没有办法得到这个想法。
我还删除了观察者:
- (void)viewWillDisappear:(BOOL)animated
{
[super viewWillDisappear:animated];
[self.mapView removeObserver:self forKeyPath:@"myLocation"];
//=> Set map delegate to nil (to avoid: mapView:regionDidChangeAnimated:]: message sent to deallocated instance )
self.mapView.delegate = nil;
self.mapView = nil;
}
并没有成功。
谁能帮我这个 ?我尝试了所有可能的解决方案,但没有办法。
提前致谢!