title: SwiftUI: avoid recreating/rerendering view in TabView with MKMapView+UIViewRepresentable
body: <p>I have a TabView with three tabs, one of which contains a map view that's implemented like this:</p>
  
  <pre class="lang-swift prettyprint-override"><code>struct MapView: UIViewRepresentable {
      let region: MKCoordinateRegion
      let animatedRegion: Bool
  
      func makeUIView(context: Context) -&gt; MKMapView {
          let mapView = MKMapView(frame: .zero)
          mapView.delegate = context.coordinator
          mapView.showsUserLocation = true
          mapView.setRegion(region, animated: animatedRegion)
          return mapView
      }
  
      func makeCoordinator() -&gt; Coordinator {
          Coordinator(self)
      }
  
      func updateUIView(_ mapView: MKMapView, context: Context) {
  
      }
  
      class Coordinator: NSObject, MKMapViewDelegate {
          var control: MapView
  
          init(_ control: MapView) {
              self.control = control
          }
      }
  }
  </code></pre>
  
  
  
  <p>The tab view is implemented like this:</p>
  
  <pre class="lang-swift prettyprint-override"><code>TabView(selection: $selection) {
      MapView(/* params */)
          .tabItem {
              Image(systemName: "1.square.fill")
              Text("map")
          }.tag(1)
      Text("Screen #2")
          .tabItem {
              Image(systemName: "2.square.fill")
              Text("2")
          }.tag(2)
      Text("Screen #3")
          .tabItem {
              Image(systemName: "3.square.fill")
              Text("3")
          }.tag(3)
  }
  </code></pre>
  
  
  
  <p>The problem is that the makeUIView(:context) method is executed every time I switch back to the map tab from one of the other two tabs. It appears that the underlying MKMapView instance is deallocated when I switch to another tab, then it's recreated when I switch back. In UIKit it doesn't rerender the whole view like that. Am I doing something wrong, or is there anything I can do to make sure that the underlying MKMapView instance is retained when I switch back so that it doesn't have to recreate it every single time?</p>