MapAnnotation SwiftUI Runtime Warnings

Hi all,

I've recently only just gotten back into using Swift and wanted to populate some markers on a Map from data stored within CoreData using SwiftUI.

In this example, the markers should display airports. I've attempted to use a simple @FetchRequest to return all the airports based on the Airport entity in CoreData and then feed this data to the annotationItems parameter within Map.

However, when I run the app, I notice that several Runtime Warnings are printed in the debug log and even more are printed when the map is moved around stating [SwiftUI] Publishing changes from within view updates is not allowed, this will cause undefined behavior.

I just wanted to ask, what am I doing wrong?

I've changed the MapAnnotation view to a simple MapMarker and the code runs fine without any warnings.

I'm sure I have missed something or done something in bad practice but I just wanted to ask here since I've been going crazy not being able to get these warnings to disappear.

I am running this on Xcode Version 14.1 beta 3 (14B5033e) using Simulator Version 14.1 (986.3), running iOS 16.1. I have also tried Xcode Version 14.0.1 (14A400) using Simulator Version 14.0.1 (986.3), running iOS 16.0. Both of these produce the same results and warnings.

The full code used is as follows:

import SwiftUI
import MapKit
import CoreData

struct AirportsView: View {
  // Fetch list of stored Airports from CoreData
  @FetchRequest(sortDescriptors: [])
  var airports: FetchedResults<Airport>
   
  @State private var region = MKCoordinateRegion(center: CLLocationCoordinate2D(latitude: 51.507222, longitude: -0.1275), span: MKCoordinateSpan(latitudeDelta: 0.5, longitudeDelta: 0.5))
   
  var body: some View {
    ZStack {
      Map(coordinateRegion: $region,
        annotationItems: airports,
        annotationContent: { airport in
          MapAnnotation(coordinate: CLLocationCoordinate2D(latitude: airport.latitude, longitude: airport.longitude)) {

              LocationMapAnnotationView()
                .shadow(radius: 10)
            }
        })
        .ignoresSafeArea()
    }
  }
}

Thank you a lot, George

Did you read this, before posting?
https://developer.apple.com/forums/thread/711899

Accepted Answer

Seems to be that this fixes it (I don't really understand why, so if someone can explain why it works and why the previous way of doing it now doesn't that would be really appreciated)

I don't take credit for this, as it was a solution by another user called tonyayoub on the Apple developer forums

This is what he found, the code that was producing the warning was:

struct AreaMap: View {

  @Binding var region: MKCoordinateRegion

  var body: some View {
    Map(coordinateRegion: $region)
  }
}

Where region was passed in the initializer of AreaMap from its parent view. This resolved the issue:

struct AreaMap: View {

  @Binding var region: MKCoordinateRegion

  var body: some View {
    let binding = Binding(
      get: { self.region },
      set: { newValue in
        DispatchQueue.main.async {
          self.region = newValue
        }
      }
    )
    return Map(coordinateRegion: binding)
  }
}

I think this is a BUG by Apple. Apple Feedback ref# FB11720091 Could some one at Apple confirm if we can safely, or how to safely ignore this purple warning?

MapAnnotation SwiftUI Runtime Warnings
 
 
Q