i have a swift code of mapkit and array of annotations but when i tried to fetch data from firebase and append its to annotation the ui not updating,i need to fetch annotations from firebase and display as annotations i mapkit
import Foundation
import MapKit
import Firebase
class EventManagerVM:Identifiable,ObservableObject{
var id = UUID()
@Published var eventName:String = ""
@Published var eventType:String = ""
@Published var annotations = [
MKPointAnnotation(
__coordinate: CLLocationCoordinate2D(latitude: 37.335480, longitude: -122.009270),
title: "Apple Campus",
subtitle: "Cupertino, CA"
)
]
func fetchEvents(){
let db = Firestore.firestore()
db.collection("events").getDocuments { [self] (querySnapshot, error) in
if let error = error {
print("Error getting documents: \(error)")
} else {
for document in querySnapshot!.documents {
let event = Event(name: document.data()["name"] as? String ?? "",
subtitle: document.data()["subtitle"] as? String ?? "",
latitude: document.data()["latitude"] as? Double ?? 0.0,
longitude: document.data()["longitude"] as? Double ?? 0.0)
let annotation = MKPointAnnotation()
annotation.title = event.name
annotation.subtitle = event.subtitle
annotation.coordinate = CLLocationCoordinate2D(latitude: event.latitude, longitude: event.longitude)
self.annotations.append(annotation)
print(annotation)
}
print("annotations count")
print(self.annotations.count)
}
}
}
}