Eureka Swift - set value of LocationRow - swift

I've got a form where a user can pick from a number of objects that includes co-ordinate data, which I want to load into a LocationRow.
I have tried several different ways to try and set the Location Row value, but it either crashes (unexpectedly found nil while unwrapping an optional Value) or doesn't reload the table with the correct data. i.e. https://i.imgur.com/2XkeHbu.png
My LocationRow eureka code:
$0.rowRight = LocationRow(){
$0.title = "Location"
$0.tag = "location"
if let page = selectedPage {
if let pageLocationLatitude = page.locationLatitude.value,
let pageLocationLongutude = page.locationLongitude.value {
print("testing for row update")
$0.value = CLLocation(latitude: pageLocationLatitude, longitude: pageLocationLongutude)
}
}
}
and the function that is called when I want to update the LocationRow
private func setSelectedPage(pageName : String) {
print("setting location of page: \(pageName)")
if pageName == username {
return
}
selectedPage = userPages?.filter("name == \"\(pageName)\"").first
if let locationLongitude = selectedPage?.locationLongitude.value,
let locationLatitude = selectedPage?.locationLatitude.value {
print("lat and lon: \(locationLatitude) \(locationLongitude)")
/*PURELY FOR TESTING
let titleRow = self.form.rowBy(tag: "title") as! TextRow
titleRow.value = "TEST WORKS OK"
titleRow.updateCell()
PURELY FOR TESTING */
let locationRow = self.form.rowBy(tag: "location") as! LocationRow
locationRow.value = CLLocation(latitude: locationLatitude, longitude: locationLongitude)
self.form.rowBy(tag: "location")?.updateCell()
}
self.tableView.reloadData()
}

From your code, I can see that you are putting this location row in a SplitRow:
// rowRight is a property of SplitRow
$0.rowRight = LocationRow(){
Therefore, the form doesn't really know about the location row. It only knows about the split row. You should get the split row using its tag first, then access rowRight.
// use the tag of your split row here!
let splitRow = self.form.rowBy(tag: "splitRow")
as! SplitRow<SomeRow, LocationRow> // replace SomeRow with the type of row on the left of the split row
let locationRow = splitRow.rowRight
locationRow.value = CLLocation(latitude: locationLatitude, longitude: locationLongitude)
locationRow.updateCell()

Related

Retrieve data from Firebase before adding to a map

I'm building an application where a user can store companies information (like name, address, latitude, longitude to a Firebase real-time database).
With that information, those companies are then presented in a map as annotations.
My viewDidLoad is shown below
override func viewDidLoad() {
super.viewDidLoad()
// Definitions
let autenticacao = Auth.auth()
let idUsuarioLogado = (autenticacao.currentUser?.uid)!
let database = Database.database().reference()
let usuarios = database.child("usuarios")
let clinicas = database.child("clinicas")
// Map - User location
self.mapa.delegate = self
self.gerenciadorLocalizacao.delegate = self
self.gerenciadorLocalizacao.desiredAccuracy = kCLLocationAccuracyBest
self.gerenciadorLocalizacao.requestWhenInUseAuthorization()
self.gerenciadorLocalizacao.startUpdatingLocation()
// retrieve user information
usuarios.child(idUsuarioLogado).observe(DataEventType.value) { (snapshot) in
let dados = snapshot.value as? NSDictionary
let emailUsuario = dados?["email"] as! String
let nomeUsuario = dados?["nome"] as! String
let perfilUsuario = dados?["perfil"] as! String
let idUsuario = snapshot.key
let usuario = Usuario(email: emailUsuario, nome: nomeUsuario, uid: idUsuario, perfil: perfilUsuario)
print("User profile \(perfilUsuario)")
}
// Clinicas listeners
clinicas.observe(DataEventType.childAdded) { (snapshot) in
let dados = snapshot.value as? NSDictionary
//print("Dados na leitura \(dados)")
let clinica = Clinica()
clinica.identificador = snapshot.key
clinica.nome = dados?["nome"] as! String
clinica.endereco = dados?["endereco"] as! String
clinica.cidade = dados?["cidade"] as! String
clinica.cep = dados?["cep"] as! String
clinica.estado = dados?["estado"] as! String
clinica.latitude = dados?["latitude"] as! String
clinica.longitude = dados?["longitude"] as! String
clinica.urlImagem = dados?["urlImagem"] as! String
clinica.idImagem = dados?["idImagem"] as! String
self.clinicasR.append(clinica)
}
// add annotations to the map
for oneObject in self.todasAnotacoes {
print("Oneobj \(oneObject)")
let umaAnotacao = MinhaAnotacao()
var oneObjLoc: CLLocationCoordinate2D = CLLocationCoordinate2DMake(oneObject.objLat, oneObject.objLong)
umaAnotacao.coordinate = oneObjLoc
umaAnotacao.title = oneObject.objName
umaAnotacao.subtitle = oneObject.objDesc
umaAnotacao.category = oneObject.objCat
self.anotacaoArray.append(umaAnotacao)
self.mapa.addAnnotations(self.anotacaoArray)
}
}
My viewDidLoad is "structured" in 4 main blocks (even though they run in a different order given they are asynchronous):
Definition;
Retrieve user profile;
Retrieve companies information (name, latitude, longitude);
Add annotations to the map.
As those are asynchronous functions, they run in different order and this is what is causing trouble to me.
note: There is still one piece missing here that is to feed all annotations to the variable todasAnotacoes which I'll do once I can retrieve data before triggering add annotation "block".
As annotation info comes from the firebase database, I should only have it executed once the clinicas.observe(DataEventType.childAdded) { (snapshot) in is concluded.
As is today, the sequence Xcode runs is:
Definitions
Add annotations;
retrieve data from firebase with companies details
I've tried adding the add annotation block to the closure, adding a dispatchqueue but none of those really worked. I also did a lot os search in stackoverflow but I couldn't find anything that i can use (or I wasn't able to understand).
So, in summary I need to run the add annotations after retrieving all data from Firebase.
Any ideas on how I could do that?
EDIT 1 - Final code with the suggested updates
override func viewDidLoad() {
super.viewDidLoad()
// Retrieving Logger user data and hidding "add" button if applicable
ProgressHUD.show("Carregando...")
let autenticacao = Auth.auth()
let idUsuarioLogado = (autenticacao.currentUser?.uid)!
let database = Database.database().reference()
let usuarios = database.child("usuarios")
var isUserLoaded = false
var isClinicsLoaded = false
usuarios.child(idUsuarioLogado).observe(DataEventType.value) { (snapshot) in
let dados = snapshot.value as? NSDictionary
let emailUsuario = dados?["email"] as! String
let nomeUsuario = dados?["nome"] as! String
let perfilUsuario = dados?["perfil"] as! String
let idUsuario = snapshot.key
let usuario = Usuario(email: emailUsuario, nome: nomeUsuario, uid: idUsuario, perfil: perfilUsuario)
isUserLoaded = true
if (isUserLoaded && isClinicsLoaded) {
self.addAnnotationsToMap();
}
//print("\(usuario.email) e \(usuario.nome) e \(usuario.uid) e \(usuario.perfil)")
}
// Option to code above
/*if Auth.auth().currentUser != nil {
if let uid = (Auth.auth().currentUser?.uid) {
let database = Database.database().reference()
let usuarios = database.child("usuarios").child(uid)
usuarios.observe(.value) { (snapshot) in
let dados = snapshot.value as? NSDictionary
let emailUsuario = dados?["email"] as! String
let nomeUsuario = dados?["nome"] as! String
let perfilUsuario = dados?["perfil"] as! String
let idUsuario = snapshot.key
let usuario = Usuario(email: emailUsuario, nome: nomeUsuario, uid: idUsuario, perfil: perfilUsuario)
print("Got here \(usuario.email) e \(usuario.nome) e \(usuario.uid) e \(usuario.perfil)")
if perfilUsuario != "admin" {
self.navigationItem.rightBarButtonItems?.remove(at: 1)
print("Disable + Button")
}
}
}
}*/
// Map - User location
self.mapa.delegate = self
self.gerenciadorLocalizacao.delegate = self
self.gerenciadorLocalizacao.desiredAccuracy = kCLLocationAccuracyBest
self.gerenciadorLocalizacao.requestWhenInUseAuthorization()
self.gerenciadorLocalizacao.startUpdatingLocation()
let clinicas = database.child("clinicas")
// Clinicas listeners
clinicas.observe(DataEventType.value) { (snapshots) in
for child in snapshots.children {
let snapshot = child as! DataSnapshot
print("Clinicas Mapeadas - end")
let dados = snapshot.value as? NSDictionary
//print("Dados na leitura \(dados)")
let clinica = Clinica()
clinica.identificador = snapshot.key
clinica.nome = dados?["nome"] as! String
clinica.endereco = dados?["endereco"] as! String
clinica.cidade = dados?["cidade"] as! String
clinica.cep = dados?["cep"] as! String
clinica.estado = dados?["estado"] as! String
clinica.latitude = dados?["latitude"] as! String
clinica.longitude = dados?["longitude"] as! String
clinica.urlImagem = dados?["urlImagem"] as! String
clinica.idImagem = dados?["idImagem"] as! String
self.clinicasR.append(clinica)
self.todasAnotacoes.append((objLat: Double(clinica.latitude) as! CLLocationDegrees, objLong: Double(clinica.longitude) as! CLLocationDegrees, objName: clinica.nome, objDesc: clinica.endereco, objId: clinica.identificador))
}
isClinicsLoaded = true
if (isUserLoaded && isClinicsLoaded) {
self.addAnnotationsToMap();
}
}
/* NOT IN USE FOR NOW
let latitude = Double(-23.623558)
let longitude = Double(-46.580787)
let localizacao: CLLocationCoordinate2D = CLLocationCoordinate2D.init(latitude: latitude, longitude: longitude)
let span: MKCoordinateSpan = MKCoordinateSpan.init(latitudeDelta: 0.01, longitudeDelta: 0.01)
let regiao = MKCoordinateRegion.init(center: localizacao, span: span)
self.mapa.setRegion(regiao, animated: true)*/
ProgressHUD.dismiss()
}
// add annotations to the map
func addAnnotationsToMap() {
anotacaoArray = []
for oneObject in self.todasAnotacoes {
for oneObject in self.todasAnotacoes {
// print("Oneobj \(oneObject)")
let umaAnotacao = MinhaAnotacao()
var oneObjLoc: CLLocationCoordinate2D = CLLocationCoordinate2DMake(oneObject.objLat, oneObject.objLong)
umaAnotacao.coordinate = oneObjLoc
umaAnotacao.title = oneObject.objName
umaAnotacao.subtitle = oneObject.objDesc
umaAnotacao.identicadorMapa = oneObject.objId
self.anotacaoArray.append(umaAnotacao)
print("Annotation added \(todasAnotacoes.count) - end")
}
self.mapa.addAnnotations(self.anotacaoArray)
self.todasAnotacoes = []
self.anotacaoArray = []
// print("Annotations added 2 - end")
}
}
The second observer in your code is using .childAdded, which means that closure gets called for each individual child node under clinicas. This makes it hard to know when you're done with clinics, so I recommend first switching that observer over to .value, with something like:
clinicas.observe(DataEventType.value) { (snapshots) in
for child in snapshots.children {
let snapshot = child as! DataSnapshot
let dados = snapshot.value as? NSDictionary
//print("Dados na leitura \(dados)")
let clinica = Clinica()
clinica.identificador = snapshot.key
clinica.nome = dados?["nome"] as! String
clinica.endereco = dados?["endereco"] as! String
clinica.cidade = dados?["cidade"] as! String
clinica.cep = dados?["cep"] as! String
clinica.estado = dados?["estado"] as! String
clinica.latitude = dados?["latitude"] as! String
clinica.longitude = dados?["longitude"] as! String
clinica.urlImagem = dados?["urlImagem"] as! String
clinica.idImagem = dados?["idImagem"] as! String
self.clinicasR.append(clinica)
}
// At this point we're done with all clinics
}
As you'll have notice this code now uses a loop to iterate over snapshots.children. So we're processing all child nodes of clinicas in one closure, which makes it much easier to know when we're done.
Now with the above change we have two closures that get called individually, and you have some code that you want to run when both of them are done. There are a few ways to synchronize this code.
The first way is what you've already tried: putting the clinicas.observe block into the closure of usuarios.child(idUsuarioLogado).observe(. Now that the clinics observer uses .value, you'll find that this is much easier to get working.
But I'd like to show an alternative below, where we use two simple flags to determine if both closures are done. For this you will have to put the code that runs after the data is loaded into a separate function, something like this:
func addAnnotationsToMap() }
for oneObject in self.todasAnotacoes {
...
}
}
Now in each of the two closures, we're going to set a flag when they're done. And then at the end of each of the closures, we'll detect whether both flags are set, and then call the new function if we have all data we need.
So at the start of viewDidLoad we define our two flags:
let isUserLoaded = false
let isClinicsLoaded = false
And then at the end of loading the user:
usuarios.child(idUsuarioLogado).observe(DataEventType.value) { (snapshot) in
let dados = snapshot.value as? NSDictionary
let emailUsuario = dados?["email"] as! String
let nomeUsuario = dados?["nome"] as! String
let perfilUsuario = dados?["perfil"] as! String
let idUsuario = snapshot.key
let usuario = Usuario(email: emailUsuario, nome: nomeUsuario, uid: idUsuario, perfil: perfilUsuario)
isUserLoaded = true
if (isUserLoaded && isClinicsLoaded) {
addAnnotationsToMap();
}
}
And similar code (setting the isClinicsLoaded flag) to the other closure.
With these in place, you'll start loading the user data and clinics when the code runs, and then whichever one of them completes last, will call the new addAnnotationsToMap function.
Another alternative is to use a DispatchGroup, which is an Apple-specific construct that can also make this much simpler. Read more about it in this answer (and the links from there): Wait until swift for loop with asynchronous network requests finishes executing

Transferring Data from Firebase into Custom Info Window

So I'm trying to transfer the data I've snapshotted from firebase into the custom info window. I currently have four different categories of activities and as such there are four different marker arrays and functions. I have one generic custom info window that I wish to display the markers Title, rating and difficulty level into.
I am having an issue Ive currently tried appending all data into one structured array and then calling the data from that array but all I get is the same set of data in all the info windows. I want each info window to specifically display the data associated with that GMS Marker.
This is is the function that shows the board activities. I have four of these functions for each activity.
func showBoardIcon() {
ref = Database.database().reference()
ref.child("location").observe(.childAdded) { (snapshot:DataSnapshot) in
if let dict = snapshot.value as? [String:AnyObject] {
if dict["Activity"] as! String == "Board" {
let longitude = dict["Longitude"] as! String
let lattitude = dict["Lattitude"] as! String
let title = dict["Title"] as! String
let key = dict["id"] as! String
self.boardIconArray.insert(coordinate(title: title, carLat: lattitude, carLng: longitude, idKey: key), at: 0)
let n = self.boardIconArray.count
let heightWidth = (self.mapView.frame.height / 12)
for var Marker in 1...n {
let boardMarker = GMSMarker()
let boardIconView = UIImage(named: "boardPin")
let image = boardIconView
let location = CLLocationCoordinate2D(latitude: Double(lattitude)!, longitude: Double(longitude)!)
boardMarker.position = location
boardMarker.icon = image
boardMarker.title = title
boardMarker.icon = self.image(image!, scaledToSize: CGSize(width: heightWidth, height: heightWidth))
func displayBoard() {
if self.boardNumber == "1" {
boardMarker.map = self.mapView
self.arrBoardMarker.append(boardMarker)
} else {
boardMarker.map = nil
}
}
displayBoard()
break
}
}
}
}
}
This is the function that displays the custom info window.
func mapView(_ mapView: GMSMapView, markerInfoWindow marker: GMSMarker) -> UIView? {
let Markerview: infoWindow = UIView.fromNib()
let a = arrCarMarkers.count
let b = arrLegMarker.count
let c = arrWaterMarker.count
let d = arrBoardMarker.count
let all = 0 + a + b + d + c
Markerview.titleLbl.text = arrAllMarkers[key].title
Markerview.ratingLbl.text = ("\(arrAllMarkers[all].rating)/5")
Markerview.difficultyLbl.text = arrAllMarkers[all].diff
Markerview.idKey.text = arrAllMarkers[all].key
transferKey = arrAllMarkers[all].key
Markerview.alpha = 0.8
Markerview.layer.cornerRadius = 30
return Markerview
}
Im not sure if what Im doing is even correct. Like I said I just want the data being snapshotted for each marker to be shown to that specific marker.
So I Managed to solve the issue.
I added
boardMarker.title = key
inside the loop where the Marker is being created.
I then wrote this section of code
func mapView(_ mapView: GMSMapView, markerInfoWindow marker: GMSMarker) -> UIView? {
let markerTitle = marker.title as! String
let Markerview: infoWindow = UIView.fromNib()
filteredStruct = arrAllMarkers.filter{$0.key.range(of: markerTitle) != nil}
print(filteredStruct)
Markerview.titleLbl.text = filteredStruct[0].title
Markerview.ratingLbl.text = filteredStruct[0].rating
Markerview.difficultyLbl.text = filteredStruct[0].diff
transferKey = markerTitle
Markerview.alpha = 0.8
Markerview.layer.cornerRadius = 30
print(transferKey)
return Markerview
}
and it works perfectly!

Button is Showing GMSMarkers but isn't Hiding it, How do I get it to do both

Im creating an app that shows different locations. Currently when a button is pressed the car locations pop up on the map, however I want to then hide those shown markers if that same button is pressed again.
This is the function that takes a snapshot of my database from firebase, it then inserts the GMSMarker into the location.
func showCarIcon() {
ref = Database.database().reference()
ref.child("location").observe(.childAdded) { (snapshot:DataSnapshot) in
if let dict = snapshot.value as? [String:AnyObject] {
if dict["Activity"] as! String == "Car" {
let longitude = dict["Longitude"] as! String
let lattitude = dict["Lattitude"] as! String
let title = dict["Title"] as! String
self.carIconArray.insert(coordinate(carLat: lattitude, carLng: longitude), at: 0)
let n = self.carIconArray.count
let heightWidth = self.mapView.frame.height
for marker in 1...n {
let carMarker = GMSMarker()
let carIconView = UIImage(named: "carPin")
let image = carIconView
let location = CLLocationCoordinate2D(latitude: Double(lattitude)!, longitude: Double(longitude)!)
carMarker.position = location
carMarker.icon = image
carMarker.title = title
carMarker.icon = self.image(image!, scaledToSize: CGSize(width: heightWidth/6, height: heightWidth/6))
func displayIt() {
if self.carNumber == "1" {
carMarker.map = self.mapView
} else {
carMarker.map = nil
}
}
displayIt()
}
}
}
}
}
So this is the action function for when button is pressed.
var carNumber = String()
#IBAction func showCar(_ sender: Any) {
if motorisedVehicleButtonActive {
motorisedVehicleButton.setImage(UIImage(named: "carO"), for: .normal)
carNumber = "1"
} else {
motorisedVehicleButton.setImage(UIImage(named: "car"), for: .normal)
carNumber = "0"
}
print(carNumber)
motorisedVehicleButtonActive = !motorisedVehicleButtonActive
showCarIcon()
}
Let me explain what is issue with your code.
You are creating new marker every time when button press. So, new marker have different object id than older.
When you try to remove it, it will not works just because of it's different marker than you placed on map.
So you need to store marker in array and on remove time, get icon from array and remove it from map.
First you need to create array of GMSMarker, because you have to store every marker which is placed on map.
So, write following line of code at top of your class.
var arrCarMarkers = [GMSMarker]()
Then after, store every marker in this array which are you placing on map.
So, update your code as follow:
func displayIt() {
if self.carNumber == "1" {
carMarker.map = self.mapView
arrCarMarkers.append(carMarker) // Here is store marker in array
} else {
carMarker.map = nil
}
}
Now, you have all marker which are placed on map. So when you want to remove these markers just update your code as follow:
#IBAction func showCar(_ sender: Any) {
if motorisedVehicleButtonActive {
motorisedVehicleButton.setImage(UIImage(named: "carO"), for: .normal)
carNumber = "1"
showCarIcon()
} else {
motorisedVehicleButton.setImage(UIImage(named: "car"), for: .normal)
carNumber = "0"
self.arrCarMarkers.forEach { $0.map = nil }
}
print(carNumber)
motorisedVehicleButtonActive = !motorisedVehicleButtonActive
}
Above code will remove all markers from map.
I hope this will works for you.

.oberve(.childAdded) producing error while .observeSingleEvent(of: .value) is not

Right now I have .observeSingleEvent(of: .value) that loads all my annotations onto the map:
func loadAnnotations() {
if let uid = Auth.auth().currentUser?.uid {
uidRef.child(uid).child("annotations").observeSingleEvent(of: .value, with: { (snapshot) in
for item in snapshot.children {
// annotationListItem is a struct I created
let annotationItem = AnnotationListItem(snapshot: item as! DataSnapshot)
let doubleLatitude = Double(annotationItem.mapViewLatitude!)
let doubleLongitude = Double(annotationItem.mapViewLongitude!)
let coordinate = CLLocationCoordinate2DMake(doubleLatitude!, doubleLongitude!)
let annotation = MKPointAnnotation()
annotation.coordinate = coordinate
annotation.title = annotationItem.annotationTitle
annotation.subtitle = annotationItem.annotationSubtitle
self.mapView.addAnnotation(annotation)
}
}, withCancel: nil)
}
}
Now I want the map to update every time the user adds a new annotation so I put in the exact same code but with .observe(.childAdded)
func annotationChildAdded() {
if let uid = Auth.auth().currentUser?.uid {
uidRef.child(uid).child("annotations").observe(.childAdded, with: { (snapshot) in
for item in snapshot.children {
let annotationItem = AnnotationListItem(snapshot: item as! DataSnapshot)
let doubleLatitude = Double(annotationItem.mapViewLatitude!)
let doubleLongitude = Double(annotationItem.mapViewLongitude!)
let coordinate = CLLocationCoordinate2DMake(doubleLatitude!, doubleLongitude!)
let annotation = MKPointAnnotation()
annotation.coordinate = coordinate
annotation.title = annotationItem.annotationTitle
annotation.subtitle = annotationItem.annotationSubtitle
self.mapView.addAnnotation(annotation)
}
}, withCancel: nil)
}
}
I get the error of:
Could not cast value of type '__NSCFString' (0x1060b84f0) to 'NSDictionary' (0x1060b92d8).
Printing description of snapshotValue:
([String : AnyObject]) snapshotValue = variable not available>
How can I fix this problem?
UPDATE
.observe(.value) works. But I am still wondering why .childAdded doesn't
The problem here is that .observeSingleEvent(of: .value) and .observe(.childAdded) don't return the same thing.
When you call .observe(.value) it returns the entire node with all the content inside at the time of the event.
But, when you use .observe(.childAdded) it would return only the content that is added to the specified path (that is, the child that is added).
You can see that by doing print(snapshot) in both methods and you will easily see the difference.
So, to access your data using .childAdded, you don't need to iterate through all the children like you would do with .observeSingleEvent(of: .value). Instead you would do :
guard let uid = Auth.auth().currentUser?.uid else {
return
}
uidRef.child(uid).child("annotations").observe(.childAdded, with: { (snapshot) in
let annotationItem = AnnotationListItem(snapshot: item as! DataSnapshot)
let doubleLatitude = Double(annotationItem.mapViewLatitude!)
let doubleLongitude = Double(annotationItem.mapViewLongitude!)
let coordinate = CLLocationCoordinate2DMake(doubleLatitude!, doubleLongitude!)
let annotation = MKPointAnnotation()
annotation.coordinate = coordinate
annotation.title = annotationItem.annotationTitle
annotation.subtitle = annotationItem.annotationSubtitle
self.mapView.addAnnotation(annotation)
}
})
Also, I would not recommend you to force cast your item like that item as! DataSnapshot because if you miss something in your database the app would just crash.
Instead, I would do it like so, using guard statements :
guard let uid = Auth.auth().currentUser?.uid else {
return
}
uidRef.child(uid).child("annotations").observe(.childAdded, with: { [weak self] (snapshot) in
let annotationKey = snapshot.key
guard let longitude = snapshot.childSnapshot(forPath: "longitude").value as? NSNumber else { return }
guard let latitude = snapshot.childSnapshot(forPath: "latitude").value as? NSNumber else { return }
guard let title = snapshot.childSnapshot(forPath: "title").value as? String else { return }
// Here it really depends on how your database look like.
// ...
// Create your annotation item using the values from the snapshot :
let annotation = AnnotationListItem(id: annotationKey, title: title, longitude: longitue, latitude: latitude)
// Finally add the data to an array :
self?.annotationsArray.append(annotation)
// Or directly to your mapView :
self?.mapView.addAnnotation(annotation)
})
Just let me know if it helps ;D
Update
For example, let's say you have initially 3 child nodes in your database. It would do something like that :
initial : observer get called :
you get child 1
you get child 2
you get child 3
now, if another child is added :
new child : observer get called :
you get child 4
etc..

Firebase database setup and data handling in Swift 3

I'm pretty new to swift and Firebase. I have database like this:
In the app I have multiple annotations and I need to pass the info from the database to them. At this point I got little bit confused with reading the data as dictionary and passing it to the annotations.
This was my previous code when I didn't use database and used Arrays:
for i in 0...2
{
let coordinate = coordinates[i]
let point = myAnnotation(coordinate: CLLocationCoordinate2D(latitude: coordinate[0] , longitude: coordinate[1] ))
point.name = names[i]
point.address = addresses[i]
point.hours = hours[i]
point.phones = phones[i]
self.mapView.addAnnotation(point)
}
You don't have to code it for me but I need at least some hint please.
I have a similar Database on my firebase, And I would do it like this:
FIRDatabase.database().reference().child("Data").observe(.value, with: {(snapshot) in
if let snapshot = snapshot.children.allObjects as? [FIRDataSnapshot] {
for snap in snapshot{
if let dict = snap.value as? [String: Any] {
if let address = dict["address"] as? String,let lat = dict["lat"] as? String, let long = dict["long"] as? String, let name = dict["name"] as? String {
let coordinate = coordinates[i]
let point = myAnnotation(coordinate: CLLocationCoordinate2D(latitude: lat , longitude: long ))
point.name = name
point.address = address
self.mapView.addAnnotation(point)
}
}
}
}
})
where I loop for each child of your Data node, pass that snap value as a dictionary, and then read the dictionary and type cast its values to see if format is correct to then assign it and append it to the array.