I have written a program in SwiftUI to display information from a database that includes the street address, but not the latitude and longitude. I've added a button that passes the street address to geocodeAddressString to get the lat & long and updates the map, but every time I click the button, even though the map appears to be displayed properly, I get a message in Xcode complaining "Modifying state during view update..."
My view has a @State variable that holds a MKCoordinateRegion() for the map to display. The closure on completion of the geocode lookup updates that @State.
@State private var mapRegion = MKCoordinateRegion()
private func updateAddress() {
let index = data.zipCode.index(data.zipCode.startIndex, offsetBy: 5)
geocoder.geocodeAddressString("\(data.streetAddress) \(data.zipCode[..<index])") { result, error in
DispatchQueue.main.async {
if let coordinate = result?.first?.location?.coordinate {
mapRegion = MKCoordinateRegion (
center: coordinate,
latitudinalMeters: 750,
longitudinalMeters: 750
)
place = IdentifiablePlace(lat: coordinate.latitude, long: coordinate.longitude)
// print ("updating address")
}
}
}
}