Swift - Firebase how to get only childs values without parent
Swift - Firebase how to get only childs values without parent
In the database i has a child cities with the name of "cities" under it and each city has multiple areas contains the coordinations
I went through every child under "cities" to find the closest location and got the city name using ref.child("cities").observe()
:
ref.child("cities").observe()
for child in snapshot.children {
let childSnap = child as! DataSnapshot
if let dicionary = childSnap.value as? [String: AnyObject]{
var latitude1 = dicionary["latitude"] as! Double
var longitude1 = dicionary["longitude"] as! Double
var altitude1 = dicionary["altitude"] as! Double
lets say the closest city is "city 2" and place it in a variable called selectedCity.
So I repeat and add it to ref.child("cities").child(selectedCity)
:
ref.child("cities").child(selectedCity)
ref.child("cities").child("(city2)").observe(.childAdded, with: { (snapshot) in
if let dicionary = snapshot.value as? [String : AnyObject] {
print(dicionary)
var altitude = dicionary["altitude"] as! Double
var latitude = dicionary["latitude"] as! Double
var longitude = dicionary["longitude"] as! Double
var theCity = dicionary["cityName"] as! String
if theCity == city2 {
locationArray.add(location.init(area: area, city: theCity, latitude: latitude, longitude: longitude, altitude: altitude))
}
}
})
Here is where i am getting the issue where im getting all the children under "cities" in "city 1" and "city 2" but skips the rest of the cities and then getting the children under "city 2" which is repeating the data
How can I get it to skip the parent "cities" and go to the child "city 2" and get the data once
1 Answer
1
I solved it in a different way then what i was trying to do.
I placed all the data in a custom array - NSMutableArray()
then lopped through that array and added an if statement (city == city2) if it is true then the row is placed in a new array
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.