I have MapView working for a single Marker. I want to access core data entity and based on the number of items it consists of, I want to create corresponding number of Markers. So inside MapView I defined Core data fetch like this:
@FetchRequest(
sortDescriptors: [NSSortDescriptor(keyPath: \LocationPublic.uniqueId, ascending: true)],
animation: .default)
private var locationPublicItems: FetchedResults<LocationPublic>
Inside MapView, while iterating over the coredata entity locationPublicItems and try to access each item I get error: on the code line (show in detail below):
for detail in self.locationPublicItems {
let latitudeStr:String = String.localizedStringWithFormat("%.15f", , detail.latitude)
....
Error:
"Accessing StateObject's object without being installed on a View. This will create a new instance each time."
CODE:
struct ProfileMapView: View {
@Environment(\.managedObjectContext) private var viewContext
@Environment(\.dismiss) var dismiss
@State var tracking:MapUserTrackingMode = .follow
@Environment(\.presentationMode) var presentationMode
var body: some View {
TabView {
NavigationView {
ZStack {
VStack (alignment: .leading){
MapView()
.ignoresSafeArea()
}
}
//.edgesIgnoringSafeArea(.all)
.safeAreaInset(edge: .bottom) {
Color.clear
.frame(height: 0)
.background(.white)
}
.toolbar {
ToolbarItem(placement: .principal) { // <3>
VStack {
Text("Profiles").font(.headline)
}
}
ToolbarItem(placement: .navigationBarLeading) {
Button(action: { presentationMode.wrappedValue.dismiss() }, label: {
HStack(spacing: 2) {
Image(systemName: "chevron.backward")
.foregroundColor(.black)
Button("Back", action: {
self.presentationMode.wrappedValue.dismiss()
} )
.foregroundColor(.blue)
}
})
}
}
}
}
}
struct MapView: UIViewRepresentable {
@FetchRequest(
sortDescriptors: [NSSortDescriptor(keyPath: \LocationPublic.uniqueId, ascending: true)],
animation: .default)
private var locationPublicItems: FetchedResults<LocationPublic>
func makeUIView(context: Context) -> MKMapView {
let view = MKMapView()
view.delegate = context.coordinator
return view
}
func updateUIView(_ uiView: MKMapView, context: Context) {
uiView.mapType = .mutedStandard
uiView.addAnnotations(self.myPins)
}
var myPins: [GpsData.MyEndPt] = []
init() {
myPins = cordToMark()
}
func cordToMark() -> [GpsData.MyEndPt] {
var myPins: [GpsData.MyEndPt] = []
var imgIndex: Int = 1
let documentsURL = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)[0]
var imageFileURL = documentsURL.appendingPathComponent("upload")
var fileName:String = String(imgIndex)
fileName.append("Image.png")
var image = UIImage(systemName: "face.smiling")
Image(uiImage: image!)
.resizable()
.aspectRatio(contentMode: .fit)
.clipShape(Circle())
.scaledToFill()
.frame(width:48, height: 48)
let data = image!.pngData()
do {
try data!.write(to: imageFileURL)
}
catch {
print("error writing image to file")
}
for detail in self.locationPublicItems {
let latitudeStr:String = String.localizedStringWithFormat("%.15f", detail.latitude) // Error here
let longitudeStr:String = String.localizedStringWithFormat("%.15f", detail.longitude)
let endPtPin = GpsData.MyEndPt(latitude: Double(latitudeStr) as! Double, longitude: Double(longitudeStr) as! Double, url: imageFileURL as! NSURL, color: "Blue")
endPtPin.endPtName = detail.locationName
myPins.append(endPtPin)
}
return myPins
}
// This calls the delegate
func makeCoordinator() -> MapViewDelegate{
let rect = CGRect(x: 0, y: 0, width: 200, height: 100)
return MapViewDelegate(frame: rect)
}
}
}
Looks like I have not defined the correct syntax for fetching core data entity items inside MapView. Can someone please tell me how to fix this?