0
votes

I am trying to use CoreData to save and retrieve markers which a user marks onto a Google Maps through using an autocomplete widget. However I keep getting a 'libc++abi.dylib: terminating with uncaught exception of type NSException' error and am not sure why or what this means? It happens when I try and store the objects into Core Data.

Here is the relevant code:

    func viewController(_ viewController: GMSAutocompleteViewController, didAutocompleteWith place: GMSPlace) {
    self.place = place
    let camera = GMSCameraPosition.camera(withLatitude: place.coordinate.latitude, longitude: place.coordinate.longitude, zoom: 15.0)
    self.vwGMap.camera = camera
    let marker = GMSMarker()
    marker.position = CLLocationCoordinate2DMake(place.coordinate.latitude, place.coordinate.longitude)
    marker.title = place.name
    marker.snippet = place.formattedAddress
    marker.map = self.vwGMap
    marker.icon = GMSMarker.markerImage(with: UIColor.blue)
    marker.tracksViewChanges = true
    self.dismiss(animated: true, completion: nil)
    print("Place name: ", place.name)
    print("Place address: ", place.formattedAddress)
    print("Place placeID: ", place.placeID)
    print("Place attributions: ", place.attributions)
    print("Place Coordinate:", place.coordinate)
    //The printing only happens in the terminal

    let newPlaceName = place.name
    self.newPlaceName = place.name
    let newPlaceAddress = place.formattedAddress
    self.newPlaceAddress = place.formattedAddress!
    let newPlacePlaceID = place.placeID
    self.newPlacePlaceID = place.placeID

    let appDelegate = UIApplication.shared.delegate as! AppDelegate
    let context = appDelegate.persistentContainer.viewContext
    let newPlace = NSEntityDescription.insertNewObject(forEntityName: "StoredPlace", into: context)

    newPlace.setValue(newPlaceName, forKeyPath: "name")
    newPlace.setValue(newPlaceAddress, forKeyPath: "address")
    newPlace.setValue(newPlacePlaceID
        , forKeyPath: "placeID")

    do
    {
        try context.save()
        print("SAVED")
    }
    catch
    {
        //PROCESS ERROR
    }


}

perhaps I should be putting the the let appDelegate etc into viewDidLoad but I end up getting the same fatal error.

I have name, address, placeID set as attributes as type 'String' in the StoredPlace entity.

more about the error: Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '+entityForName: nil is not a legal NSManagedObjectContext parameter searching for entity name 'StoredPlace'' *** First throw call stack:

What does this error mean and how do i get around it?

2

2 Answers

0
votes

where is your Core Data methods ? in this ViewController or in AppDelegate ?

0
votes

I've worked this out now and it is rather silly but an easy mistake to make so FYI to others. If after creating the xcdatamodel you rename it do not forget to change that in the Appdelegate where you state the container.So 'let container = NSPersistentContainer(name: "YOUR RENAMED FILE")', otherwise you get the error stated above!