2017-08-15 245 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

相關問題