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