Vincenzo
Vincenzo

Reputation: 6358

function returns after changing Firebase observer SWIFT 4

I'm rewriting my firebase functions to put in it's own function the realtime part of getting data fro database, so I changed from .observe(.childAdded, with: {(snapshot) in to .observeSingleEvent(of: .value, with: { (snapshot) in in referencing to database. The function now is returning on guard let data = snapshot.value as? [String :String] else { return }when it wasn't before.. What has changed when snapshot is the same? Any explanation of it wold be great as I don't see it myself. Many thanks as always.

Here are the two versions of the function:

Old observer:

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)
        })

    }

New observer:

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)

        })
    }
}

EDIT:

here are the prints from snapshots so to see results of the two versions:

observeSingleEvent snapshot:

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";
    };
}

and the childAdded snapshots:

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";
}

Upvotes: 0

Views: 71

Answers (3)

Vincenzo
Vincenzo

Reputation: 6358

This is just a partial answer but after lots of different tries and with the help of Sh_Khan we got to make it work. The problem was in the line guard let data = snapshot.value as? [String :Any] else { return }that needed to be guard let data = snapshot.value as? [String : [String : String] else { return }.

so the function got to be written as:

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)

            })
        }
    }

Now there are two things I'm left to resolve. First: How to get the child key as the line guard let firebaseKey = snapshot.key as? String else { return }gives me the name of the node instead? Second: The newer function I created just to get newerAlerts with .observe(.childAdded, with: { (snapshot) in does not get called whe a new child is added to the node. Should I ask new questions or continue here?

Thank you

Upvotes: 0

Shehata Gamal
Shehata Gamal

Reputation: 100503

Change

guard let data = snapshot.value as? [String :String] else { return }

to

guard let data = snapshot.value as? [String :[String:String]] else { return } 
data.values.forEach {  
    print($0["Latitude"]) 
    print($0["Longitude"])  
}

Upvotes: 1

Eduardo Leite
Eduardo Leite

Reputation: 71

Can you show an exemple on how the json is beeing stored in database? with

.observe(.childAdded, with: {(snapshot) in

you receive only the child that was added, and with

.observeSingleEvent(of: .value, with: { (snapshot) in

you receive the whole node, so maybe the node is not [String:String] its probably more then one item so it should be [String:Any] and then you can get every child from within it.

Upvotes: 0

Related Questions