Bonnke Bonnke - 1 year ago 73
Objective-C Question

Crash while getting my location on GoogleMaps on iPhone

I am writing you because I am stuck with a super strange error.
I tried to add GMS on my app, and finally, when I have to get device location, I am stuck with this crash:

Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'An instance 0x7ff7fd82def0 of class GMSMapView was deallocated while key value observers were still registered with it. Current observation info: (
Context: 0x0, Property: 0x7ff7fdc2f470>

Code is initialized here:

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

- (void)observeValueForKeyPath:(NSString *)keyPath
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

and still no success. I put breakpoint in observerValueForKey method, and once is out from here, crash appears. No way to get the idea.

I also removed observer :

- (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;

and no success.

Can anyone help me with this ? I tried all possible solutions, but no way.

Thanks in advance!

Answer Source

How already said in comment.. the GMSMapView must be Strong Your code will not work in iOS 8 if you not call requestWhenInUseAuthorization

I recommend you to use only the CLLocationManager instead KVO... but is your decision.

Attached my example - I modified the Google example... with 2 example:

  1. with KVO
  2. only with CLLocationManager

If you have any issues... just let me know

PS Don't forget to add you API Key

Happy coding!


if you startUpdatingLocation.. don't forget to stop it somewhere with stopUpdatingLocation.. let say in ViewDidDisapear