I am working on integrating the GoogleMaps iOS SDK into my project and I keep getting this error:
'NSInternalInconsistencyException' 'An instance 0x7fea5e93e210 of class GMSMapView was deallocated while key value observers were still registered with it. Current observation info:
Here's my view controller code:
import UIKit
import GoogleMaps
class ViewController: UIViewController, CLLocationManagerDelegate, GMSMapViewDelegate {
var locationManager = CLLocationManager()
var didFindMyLocation = false
let mapView = GMSMapView()
override func viewDidLoad() {
super.viewDidLoad()
let mapView = GMSMapView()
locationManager.delegate = self
locationManager.requestWhenInUseAuthorization()
let camera: GMSCameraPosition = GMSCameraPosition.cameraWithLatitude(37.61729,longitude: -122.38229, zoom: 18)
mapView.camera = camera
mapView.delegate = self
mapView.addObserver(self, forKeyPath: "myLocation", options: NSKeyValueObservingOptions.New, context: nil)
}
override func observeValueForKeyPath(keyPath: String?, ofObject object: AnyObject?, change: [String : AnyObject]?, context: UnsafeMutablePointer<Void>) {
if !didFindMyLocation {
let myLocation: CLLocation = change![NSKeyValueChangeNewKey] as! CLLocation
mapView.camera = GMSCameraPosition.cameraWithTarget(myLocation.coordinate, zoom: 18.0)
mapView.settings.myLocationButton = true
didFindMyLocation = true
}
}
}
Just as it says: "key value observers were still registered with it", which is essentially complaining that you didn't un-register the KVO observer.
KVO, in this sense, is kinda like NSNotification
- if you registered an observer in, say, viewDidLoad:
, you need to remove the observer in, say, viewDidDisappear:
or dealloc
.
In your case, try to add dealloc
function and use removeObserver:forKeyPath:context:
to unsubscribe from KVO. Read more at mattt's article for KVO here.