IOS Swift - Get values from different nodes in Firebase database -
i'd list of participants of event, in events node, id# of employee listed. achieve name associated id # employee node. here's structure of dbase.
events: uuid: name: summer festival 2017 site: vegas participants: employeeid1: true employeeid2: true employee: employeeid1: name: gary department: video production
and here's code
@ibaction func downloadbuttonaction(_ sender: uibutton) { ref.child("events").child(eventkeylabel.text!).child("participants").observesingleevent(of: .value, with: {(snapshot) in // dictionary snapshot if let participantsdict = snapshot.value as? [string : anyobject] { each in participantsdict { let employeeidnum = each.key let employeename = self.getname(foruid: employeeidnum) self.employeeid.append(employeeidnum) self.name.append(employeename) } print(self.employeeid) print(self.name) } }) // end observesingleevent } func getname(foruid userid: string) -> string{ var empname = "" ref.child("employee").child(userid).observe(dataeventtype.value, with: { (snapshot) in if let value = snapshot.value as? nsdictionary { empname = (value["name"] as? string)! } }) return empname }
results:
["0001", "0002", "0003", "0004"] ["", "", "", ""]
full code:
@ibaction func downloadbuttonaction(_ sender: uibutton) {
ref.child("events").child(eventkeylabel.text!).child("participants").observesingleevent(of: .value, with: {(snapshot) in // dictionary snapshot if let participantsdict = snapshot.value as? [string : anyobject] { each in participantsdict { let employeeidnum = each.key self.getname(foruid: employeeidnum) { empname in self.name.append(empname) print(empname) } self.employeeid.append(employeeidnum) } print(self.employeeid) print(self.name) } }) // end observesingleevent
}
func getname(foruid userid: string, callback: @escaping (string)->void){
ref.child("employee").child(userid).observe(dataeventtype.value, with: { (snapshot) in if let value = snapshot.value as? nsdictionary { print(value) if let empname = value["name"] as? string{ callback(empname) } } })
}
it doesn't return employee names. please help. thank you!
problem since fetching firebase asynchronous call, have use callback name function calling getname method, can this:
@ibaction func downloadbuttonaction(_ sender: uibutton) { ref.child("events").child(eventkeylabel.text!).child("participants").observesingleevent(of: .value, with: {(snapshot) in // dictionary snapshot if let participantsdict = snapshot.value as? [string : anyobject] { each in participantsdict { let employeeidnum = each.key self.getname(foruid: employeeidnum) self.employeeid.append(employeeidnum) } print(self.employeeid) print(self.name) } }) // end observesingleevent } func getname(foruid userid: string) { ref.child("employee").child(userid).observe(dataeventtype.value, with: { (snapshot) in if let value = snapshot.value as? nsdictionary { print(value) //add here if let empname = value["name"] as? string{ self.name.append(empname) } } }) }
Comments
Post a Comment