我从快照Swift 4

时间:2019-03-02 11:39:42

标签: swift firebase firebase-realtime-database

我正在从Firebase数据库中检索节点中的帖子,但是我得到了Node键而不是Child键,我将使用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)

            })
        }
    }

1 个答案:

答案 0 :(得分:0)

我终于明白我的错误在哪里。我显然是在获取父密钥。 在for in的{​​{1}}循环中,我声明了一个变量来保存子级快照值,然后像以前一样获取我的值。我只是没有意识到我必须输入从Firebase回来的字典作为快照来读取条目。我希望这会对其他初学者产生帮助,因为这对我来说是很大的挫败感,但是像往常一样,学习艰苦的方法可以节省大量时间。

因此,完整的重写功能现在是:

snapshot.children

我不了解的一件事是投反对票,如果他们至少留下评论以帮助改善问题,那将是有用的,否则只会加剧已经来找人的挫败感他面临的一个问题。 希望最后的评论也有帮助。这是一个很棒的社区,如果没有这一点,我真的无法做到这一点。 成为其中的一员,我们感到非常自豪。 干杯。

相关问题