【发布时间】:2019-03-02 11:39:42
【问题描述】:
我正在从 Firebase 数据库中检索节点中的帖子,但我得到的是 Node 键而不是 Child 键,我将使用它从数据库中删除条目。我得到键值 I guard let firebaseKey = snapshot.key as? String else { return } 的行。我试过snapshot.children,但没有key参数可供选择。那我该怎么做呢?
像往常一样非常感谢。
完整的功能是:
func displayAlerts(setCompletion: @escaping (Bool) -> ()) {
print(" MapArray.alertNotificationCoordinatesArray before 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) -> Void in
print(" snapshot is: \(snapshot)")
guard let data = snapshot.value as? [String :[String:String]] else { return }
guard let firebaseKey = snapshot.key as? String else { return }
data.values.forEach {
// let firebaseKey = data.keys[]
let dataLatitude = $0["Latitude"]!
let dataLongitude = $0["Longitude"]!
let type = $0["Description"]!
let id = Int($0["Id"]!)
print("firebaseKey is:\(firebaseKey)")
print("dataLatitude is: \(dataLatitude)")
print("dataLongitude is: \(dataLongitude)")
print("type is: \(type)")
print("id is: \(id)")
print("Key is: \(firebaseKey)")
print("data is: \(data)")
let doubledLatitude = Double(dataLatitude)
let doubledLongitude = Double(dataLongitude)
let recombinedCoordinate = CLLocationCoordinate2D(latitude: doubledLatitude!, longitude: doubledLongitude!)
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)
}
print("Firebase alerts posts retrieved")
print(" MapArray.alertNotificationCoordinatesArray after displayAlert snapshot is: \(MapArray.alertNotificationCoordinatesArray)")
print(" self.userAlertNotificationArray after displayAlert snapshot is: \(self.userAlertNotificationArray)")
self.mapView.addAnnotations(self.userAlertNotificationArray)
setCompletion(true)
})
}
}
【问题讨论】:
标签: swift firebase firebase-realtime-database