My Locations: mapView.addAnnotations gives EXC_BAD_ACCESS

When I try to run the My Locations app on the simulator it works well, but using it on the device it always gives an error at the mapView.addAnnotations(locations) line.
The Location class conforms to the MKAnnotation protocol, and basically everything is like in the book. I’m at the Map chapter.
Do you guys have any idea what causes this error?

It’s a little difficult to guess at what might be going wrong based on a description of the issue - except in the case of some fairly well-known issues :slight_smile: One thing you can do is check your project against the provided final project in the book if you want to troubleshoot the issue yourself.

Alternatively, please upload your project as a ZIP file somewhere and provide a link to the ZIP file so that I (or someone else) can download the file and see what might be going on.

Thanks for the tip fahim! I checked it with the source code in the zip file and it throws the same error. So it is not code based error but something else.
I’m using iPhone XS if it matters.

I just tested the code from the book on a device and I could add a location and then have it display on the map. There were no issues and no crashes. So it appears it is either an app/device setting (like you not agreeing to the initial location permissions dialog) or something subtle in the code elsewhere.

You can try deleting the app off your device, installing it again, and agreeing to the prompts. Or you can upload the project code as I mentioned to see if somebody else can replicate the crash.

Thank you for your input, I really appreciate your help. This app though does not allow you to tag locations and use them on the map view unless you enable the location services. So this error can not stem from me not agreeing on the permission-requests.
Uploading my code is completely useless, if even the code form the book doesn’t work on my device.
My guess is that since I use the latest device I need to do something in the Settings, I was hoping someone had an idea what to do exactly.

Just to inform anybody about the latest development of the issue, the code seems to be working fine on the 6s. So it looks like to be a device-dependant issue.

1 Like

In simulator is working, but in real iPhone XS is not.

With this workaround is not crashing but it is not fixed.

class MyAnotation: NSObject, MKAnnotation{
  var coordinate: CLLocationCoordinate2D
  var title: String?
  var subtitle: String?
  
  init(withLocation location: Location){
    self.coordinate = location.coordinate
    self.title = location.title
    self.subtitle = location.subtitle
    
    super.init()
  }
}

      // edited method
  func updateLocations() {
    let myOldAnottations = locations.map({MyAnotation(withLocation: $0)})
    mapView.removeAnnotations(myOldAnottations)
    //mapView.removeAnnotations(locations)
    let entity = Location.entity()
    let fetchRequest = NSFetchRequest<Location>()
    fetchRequest.entity = entity
    locations = try! managedObjectContext.fetch(fetchRequest)
    let myAnottations = locations.map({MyAnotation(withLocation: $0)})
    mapView.addAnnotations(myAnottations)
    //mapView.addAnnotations(locations)
  }

I can replicate the crash using the code from the book on an iPhone Xs max. It works fine on an iPhone 6 and original iPad Pro, but it crashes when calling addAnnotations on the Xs max.

While the workaround doesn’t crash on the Xs max, the reason it’s not working is back in the extension in the MapViewControoler, you have the guard statement where the annotation is no longer a Location object, so it returns nil at that point.

This topic was automatically closed after 166 days. New replies are no longer allowed.