I am working on SwiftUI and displaying MapView using UIViewRepresentable. Now, when map is loaded then I am calculating route between two points and i am storing that route in model.
class MapInfo: ObservableObject {
@Published var routeArray: [RouteArray] = []
}
I am calling the calculateRoute() function in UIViewRepresentable MapCoordinator
struct MapView: UIViewRepresentable {
@StateObject var mapInfo = MapInfo()
}
func requestRoute() {
// when response is received
parentView.mapView.mapInfo.routeArray = responseArray
}
But My problem is SwiftUI View onReceive is not getting called when MapInfo @Published RouteArray is updated.
I have added my code.
struct MapContainerView: View {
@ObservedObject var mapInfoObj = MapInfo()
var body: some View {
ZStack {
MapView()
}.onReceive(mapInfoObj.$routeArray) { updatedRouteArray in
print(updatedRouteArray)
}
}
}
Any help with this is highly appreciated. Thank you :)
MapInfoinstances. You need to pass the one from yourMapContainerViewto yourMapViewvia its initialiser.