Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

GoogleMaps iOS SDK GMSMapView Class Deallocated

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
        }
    }

}
like image 745
Ryan Diew Avatar asked Sep 11 '26 14:09

Ryan Diew


1 Answers

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.

like image 159
Stephenye Avatar answered Sep 13 '26 02:09

Stephenye



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!