Rama Krish
Rama Krish

Reputation: 373

SwiftUI Onappear Modifying state during view update, this will cause undefined behavior

I am rendering Map in SwiftUI, updating the annotations from api response on appear, and I get Modifying state during view update, this will cause undefined behavior.

Below is the code

class ViewModel: ObservableObject {
     @Published var results = [Destination]()
     func getData() {
         // APi call
         self.results = response
     } 
}

struct SampleView: View {

    @ObservedObject var viewModel = ViewModel()
    var body: some View {
    if #available(iOS 14.0, *) {
        VStack(spacing: -5) {
            Map(coordinateRegion: $region, showsUserLocation: true, annotationItems: viewModel.categorySearchResults) { dest in
                MapMarker(coordinate: dest.coordinate)
            }
        }.background(Color.clear)
        .onAppear(perform: {
            DispatchQueue.main.async {
                setRegion(viewModel.getLatLong()!)
                viewModel.getData()
            }
        })
    } else {
    }
}

private func setRegion(_ coordinate: CLLocationCoordinate2D) {
    region = MKCoordinateRegion(
        center: coordinate,
        span: MKCoordinateSpan(latitudeDelta: 0.2, longitudeDelta: 0.2)
    )
}

}

Upvotes: 1

Views: 1288

Answers (1)

Ram
Ram

Reputation: 289

I see this issue because the map is updating on State variable region as well the @Published property of results, so it is triggering the warning.

replace $region with .constant(region) Just use the below code that will solve the issue

But if you update the region value, it won't update the Map or you can send the viewModel.categorySearchResults as a dependency from the previous screen so that you don't need to call the viewModel.getData() in the onAppear method

  Map(coordinateRegion: .constant(region), showsUserLocation: true, annotationItems: viewModel.categorySearchResults) { dest in
                    MapMarker(coordinate: dest.coordinate)
                }

Upvotes: 1

Related Questions