Я переписываю свои функции firebase, чтобы добавить в свою функцию часть получения данных из базы данных в реальном времени, поэтому я изменил значение с .observe(.childAdded, with: {(snapshot) in
на .observeSingleEvent(of: .value, with: { (snapshot) in
при обращении к базе данных.Теперь функция возвращается на guard let data = snapshot.value as? [String :String] else { return }
, когда ее не было раньше. Что изменилось, когда снимок остался прежним?Любое объяснение этого было бы замечательно, поскольку я сам не вижу этого.Большое спасибо как всегда.
Вот две версии функции:
Старый наблюдатель:
func displayAlerts(setCompletion: @escaping (Bool) -> ()) {
self.mapView.removeAnnotations(mapView.annotations)
MapArray.alertNotificationCoordinatesArray.removeAll()
self.userAlertNotificationArray.removeAll()
print(" MapArray.alertNotificationCoordinatesArray before snapshot is: \(MapArray.alertNotificationCoordinatesArray)")
print(" self.userAlertNotificationArray before snapshot is: \(self.userAlertNotificationArray)")
ref = Database.database().reference()
databaseHandle = ref?.child("Continent").child("Europe").child("Country").child("Italy").child("Region").child("Emilia-Romagna").child("City").child("Bologna").child("Community").child("Alert Notifications").observe(.childAdded, with: { (snapshot) in
// self.mapView.removeAnnotations(self.mapView.annotations) //
print(" snapshot is: \(snapshot)")
guard let data = snapshot.value as? [String:String] else { return }
guard let firebaseKey = snapshot.key as? String else { return }
// let date = data!["Date"]
// let time = data!["Time"]
let dataLatitude = data["Latitude"]!
let dataLongitude = data["Longitude"]!
let type = data["Description"]!
let id = Int(data["Id"]!)
let doubledLatitude = Double(dataLatitude)
let doubledLongitude = Double(dataLongitude)
let recombinedCoordinate = CLLocationCoordinate2D(latitude: doubledLatitude!, longitude: doubledLongitude!)
print("Firebase alerts posts retrieved")
let userAlertAnnotation = UserAlert(type: type, coordinate: recombinedCoordinate, firebaseKey: firebaseKey, title: type,id: id!)
self.userAlertNotificationArray.append(userAlertAnnotation) // array of notifications coming from Firebase
// print("userAlertNotificationArray after retrieving from Firebase is : \(self.userAlertNotificationArray)")
MapArray.alertNotificationCoordinatesArray.append(recombinedCoordinate) // array for checkig alerts on route
print(" MapArray.alertNotificationCoordinatesArray after snapshot is: \(MapArray.alertNotificationCoordinatesArray)")
print(" self.userAlertNotificationArray after snapshot is: \(self.userAlertNotificationArray)")
setCompletion(true)
self.mapView.addAnnotations(self.userAlertNotificationArray)
})
}
Новый наблюдатель:
func displayAlerts(setCompletion: @escaping (Bool) -> ()) {
print(" MapArray.alertNotificationCoordinatesArray before newer displayAlert snapshot is: \(MapArray.alertNotificationCoordinatesArray)")
print(" self.userAlertNotificationArray before displayAlert snapshot is: \(self.userAlertNotificationArray)")
if self.userAlertNotificationArray.count == 0 {
ref = Database.database().reference()
ref?.child("Continent").child("Europe").child("Country").child("Italy").child("Region").child("Emilia-Romagna").child("City").child("Bologna").child("Community").child("Alert Notifications").observeSingleEvent(of: .value, with: { (snapshot) in
// self.mapView.removeAnnotations(self.mapView.annotations) //
print(" snapshot is: \(snapshot)")
guard let data = snapshot.value as? [String :Any] else { return }
guard let firebaseKey = snapshot.key as? String else { return }
// let date = data!["Date"]
// let time = data!["Time"]
let dataLatitude = data["Latitude"] as! Double
let dataLongitude = data["Longitude"] as! Double
let type = data["Description"] as! String
let id = Int(data["Id"] as! String)
let doubledLatitude = Double(dataLatitude)
let doubledLongitude = Double(dataLongitude)
let recombinedCoordinate = CLLocationCoordinate2D(latitude: doubledLatitude, longitude: doubledLongitude)
print("Firebase alerts posts retrieved")
let userAlertAnnotation = UserAlert(type: type, coordinate: recombinedCoordinate, firebaseKey: firebaseKey, title: type,id: id!)
self.userAlertNotificationArray.append(userAlertAnnotation) // array of notifications coming from Firebase
MapArray.alertNotificationCoordinatesArray.append(recombinedCoordinate) // array for checkig alerts on route
print(" MapArray.alertNotificationCoordinatesArray after newer displayAlert snapshot is: \(MapArray.alertNotificationCoordinatesArray)")
print(" self.userAlertNotificationArray after newer displayAlert snapshot is: \(self.userAlertNotificationArray)")
self.mapView.addAnnotations(self.userAlertNotificationArray)
setCompletion(true)
})
}
}
РЕДАКТИРОВАТЬ:
вот распечатки со снимков, чтобы увидеть результаты двух версий:
снимок наблюдатьSingleEvent:
snapshot is: Snap (Alert Notifications) {
"-LZtTuFSKMhhXFwyT-7K" = {
Description = "Ciclabile chiusa";
Id = 0;
Latitude = "44.50139187990401";
Longitude = "11.33592981426992";
};
"-LZtUV8MOxVrvPnEfi4g" = {
Description = "Lavori in corso";
Id = 1;
Latitude = "44.5013918797401";
Longitude = "11.335929814371545";
};
"-LZtV7sJJrOQjAimszTm" = {
Description = "Pericoli sulla ciclabile";
Id = 2;
Latitude = "44.50139187974223";
Longitude = "11.335929814367324";
};
}
и снимки дочерних добавленных:
snapshot is: Snap (-LZtTuFSKMhhXFwyT-7K) {
Description = "Ciclabile chiusa";
Id = 0;
Latitude = "44.50139187990401";
Longitude = "11.33592981426992";
}
snapshot is: Snap (-LZtUV8MOxVrvPnEfi4g) {
Description = "Lavori in corso";
Id = 1;
Latitude = "44.5013918797401";
Longitude = "11.335929814371545";
}
snapshot is: Snap (-LZtV7sJJrOQjAimszTm) {
Description = "Pericoli sulla ciclabile";
Id = 2;
Latitude = "44.50139187974223";
Longitude = "11.335929814367324";
}