在Swift 3中从Firebase检索数据时出现问题

时间:2016-11-14 03:02:12

标签: swift firebase firebase-realtime-database

我试图从firebase检索数据,将其存储在一个数组中,然后将其用作另一个firebase查询的引用。不幸的是,我似乎无法让它正常工作。底部的打印功能始终为空。我试图在for循环之后调用每个连续的函数,但我可能做错了。

我在firebase中有2个分支,看起来有点像这样:

-id
 |
 Group1
   |
   ID01: Created on 01.01.16


-idDetails
 |
 ID01
  //name: name01
  //description: description01 

这是我的代码:

var array = [String]()
var id = [String]()
var items = [Item]() //Item Class Array

override func viewDidLoad() {
    super.viewDidLoad()
    getIdFromFirebase()


}

//first task
func getIdFromFirebase(){
    for index in array //index is being used as part of path query
    {
     FireDbase_Main.child("Group1").child("id").child(index).observe(FIRDataEventType.childAdded, with: { (snapshot) in
            self.id = (snapshot.key)
            id.append(id)
        })
    getDataForItems()
    }
 }

//second task
func getDataForItems() {
    let newItem = Item()
    for index in id //i is being used as part of path query
    {
    FireDbase_Main.child("idDetails").child(index).observeSingleEvent(of: .value, with: { (snapshot) in
    newItem.itemID = snapshot.key
    newItem.name = (snapshot.value as! NSDictionary)["name"] as! String
    newItem.desc = (snapshot.value as! NSDictionary)["description"] as! String
    self.items.append(newItem)
  }
printItemsArray(
}
   //3rd Task: For now I have put a print here just as a placeholder task.
 func printItemsArray(){
   print(items.map {$0.itemID})
   print(items.map {$0.name})
   print(items.map {$0.desc})
}

0 个答案:

没有答案
相关问题