2017-08-15 231 views
1

我想要获取事件参与者列表,在事件节点中,只列出员工的ID号。我想实现的是从员工节点获取与该ID#关联的名称。这是我的dbase的结构。IOS Swift - 从Firebase数据库中的不同节点获取值

events: 
    uuid: 
     name: summer festival 2017 
     site: vegas 
     participants: 
      employeeID1: true 
      employeeID2: true 

Employee: 
    employeeID1: 
     name: gary 
     department: video production 

,这里是我的代码

@IBAction func downloadButtonAction(_ sender: UIButton) { 

     ref.child("events").child(eventKeyLabel.text!).child("participants").observeSingleEvent(of: .value, with: {(snapshot) in 

      // get the dictionary from the snapshot 
      if let participantsDict = snapshot.value as? [String : AnyObject] { 


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

结果:

["0001", "0002", "0003", "0004"] 
["", "", "", ""] 

全码:

@IBAction FUNC downloadButtonAction(_发件人:UIButton的){

ref.child("events").child(eventKeyLabel.text!).child("participants").observeSingleEvent(of: .value, with: {(snapshot) in 

    // get the dictionary from the snapshot 
    if let participantsDict = snapshot.value as? [String : AnyObject] { 


     for 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用户名:字符串,回调:@escaping(字符串) - >无效){

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

}

不退还员工姓名。请帮忙。谢谢!

+0

请在empName =(value [“name”]后面添加print(empName)as String)! getName函数中的语句来查看empName是否存在 – 3stud1ant3

+0

您好,感谢您的回复。我确实尝试了它,并且它返回了名称,但它在打印名称后崩溃。它打印它,但不返回它 – TSM

+0

请检查我的答案,看看它是否有帮助,如果您遇到问题,请在评论中提问 – 3stud1ant3

回答

0

问题是,由于从火力此取是一个异步调用,所以你必须使用回调以获取名称到您所呼叫的GetName方法的功能,你可以做这样的事情:

@IBAction func downloadButtonAction(_ sender: UIButton) { 

     ref.child("events").child(eventKeyLabel.text!).child("participants").observeSingleEvent(of: .value, with: {(snapshot) in 

      // get the dictionary from the snapshot 
      if let participantsDict = snapshot.value as? [String : AnyObject] { 


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

       } 
      } 
     }) 


    } 
+0

它将print(self.name)打印到下面的print(empName)时打印所有的名字,所以我想它被添加到名称数组中,我必须仔细检查。实际上,当点击按钮时,它应该循环遍历所有参与者数据并将其转换为excel文件并将其发送到电子邮件,而不是将其显示给UITableView – TSM

+0

您好,我发现我的代码没有任何问题。问题在于:关键名称。我现在可以下载csv格式的参与者列表并发送给电子邮件。如果没有你的帮助,我无法做到,所以非常感谢你! – TSM

+0

@TSM我很高兴我可以帮助,祝贺,我希望你已经了解了异步和同步呼叫,回调等请了解这些主题,这些都是在ios开发中的重要主题,快乐编码:) – 3stud1ant3

相关问题