2017-09-24 173 views
1

我試圖讓從具有節點多一個層次,並通過它不能得到一個節點的結果:火力地堡循環多級節點

我檢索這樣的數據(它完美,但不是爲節點所謂的 「項目」):

func fetchMeals() { 
    print("start pulling data") 

    let user = Auth.auth().currentUser?.uid 

    ref.child("Users_Food_Data").child(user!).queryOrderedByKey().observeSingleEvent(of: .value, with: { snapshot in 

     if snapshot.value as? [String : AnyObject] != nil { 

      let allMeals = snapshot.value as! [String : AnyObject] 
      self.foodArray.removeAll() 

      for (_, value) in allMeals { 

       let foodToShow = FoodStruct() 


       if let calories = value["calories"] as? String, 
        let itemCarbs = value["energy"] as? String, 
        let itemProteins = value["proteins"] as? String, 
        let itemFat = value["fat"] as? String, 
        let date = value["date"] as? String, 
        let id = value["id"] as? String, 
        let name = value["name"] as? String, 
        let interval = value["interval"] as? Int { 
        foodToShow.itemKcal = calories 
        foodToShow.itemCarbs = itemCarbs 
        foodToShow.itemProteins = itemProteins 
        foodToShow.itemFat = itemFat 
        foodToShow.id = id 
        foodToShow.interval = interval 
        foodToShow.date = date 
        foodToShow.itemName = name 



        self.foodArray.append(foodToShow) 
        self.collectionView?.reloadData() 
       } 

       self.breakfastArray.sort(by: {$0.interval! > $1.interval!}) 



      } 
     } 

     }) 
} 

和數據庫看起來是這樣的:

"Users_Food_Data" : { 
"JztkBihGgda0jtSpe6pNwt8hZu13" : { 
    "Breakfast:23 Sep 2017" : { 
    "calories" : "1145.0", 
    "date" : "23 Sep 2017", 
    "energy" : "238.8", 
    "fat" : "3.0", 
    "id" : "-Kukx_9lSpCh3lcEMzap", 
    "interval" : 1.506207565807117E9, 
    "items" : { 
     "-KukxKAntXDaS__v3ZLA" : { 
     "calories" : "30", 
     "date" : "23 Sep 2017", 
     "energy" : "6", 
     "fat" : "0.1", 
     "interval" : 1.506207500336909E9, 
     "itemKey" : "-KukxKAntXDaS__v3ZLA", 
     "mealKey" : "-KukxKAntXDaS__v3ZLB", 
     "name" : "Strawberries", 
     "proteins" : "0.8", 
     "quantity" : "3" 
     }, 
     "-KukxLYmyg32lU1D3Wh3" : { 
     "calories" : "29", 
     "date" : "23 Sep 2017", 
     "energy" : "9", 
     "fat" : "0.5", 
     "interval" : 1.506207505968336E9, 
     "itemKey" : "-KukxLYmyg32lU1D3Wh3", 
     "mealKey" : "-KukxLYmyg32lU1D3Wh4", 
     "name" : "Lemon", 
     "proteins" : "1.1", 
     "quantity" : "1" 
     } 
    }, 
    "name" : "Breakfast", 
    "proteins" : "17.0" 
    }, 
    "Breakfast:24 Sep 2017" : { 
    "calories" : "959.0", 
    "date" : "24 Sep 2017", 
    "energy" : "106.4", 
    "fat" : "46.1", 
    "id" : "-KunWOZeSxW9eCIA6O1z", 
    "interval" : 1.506250519537633E9, 
    "items" : { 
     "-KulrJq6jOpsG6oiJuDM" : { 
     "calories" : "458", 
     "date" : "24 Sep 2017", 
     "energy" : "4.6", 
     "fat" : "45", 
     "interval" : 1.506222704055992E9, 
     "itemKey" : "-KulrJq6jOpsG6oiJuDM", 
     "mealKey" : "-KulrJq6jOpsG6oiJuDN", 
     "name" : "Coconut", 
     "proteins" : "4", 
     "quantity" : "1" 
     }, 

我怎樣做,當我只知道 「Users_Food_Data」 和用戶ID。我只想將它們列在apropiate細胞中。

回答

1

這是我的建議。

首先看你的數據庫結構,你現在有這樣的事情:

Users_Food_Data 
|_userid 
    |_FoodStruct 
    |_items 
     |_id_1 
     |_data like calories, date... 
     |_id_2 
     |_data... 

有太多的節點,這種層次的不尊重我們所說非規範化。請記住,Firebase數據庫就像NoSQL數據庫,與SQL經典數據庫相比,它有很大的不同。下面是一個解釋的最佳實踐進行非規範化的數據庫結構:Structure your database

什麼,我可以建議是構建數據庫,更是這樣的:

One node : 
UsersFood 
|_userid 
    |_breakfeast_id_1 
    |_breakfeast_id_2... 

2nd node 
Breakfeasts 
|_breakfeast_id_1 
    |_item_id_1 
    |_item_id_2... 

3rd node 
Items 
|_item_id_1 
    |_calories 
    |_date 
    |_energy... 

然後在你的代碼,你可以:

  1. UsersFood節點上有一個單一的事件遵守所有的breakfeast IDS
  2. 然後用所有breakfeast IDS得到items_id
  3. 終於得到items_id數據

希望它能幫助你通過你的代碼。

編輯1:您還可以遍歷子節點通過使用這樣的:

for (DataSnapshot snapshot : dataSnapshot.getChildren()) { 
    //DO WHAT YOU NEED TO DO 
} 
+0

謝謝!我知道非規範化,我像你說的那樣構建我的數據。在這種情況下,我必須直接從節點獲取數據。我非常肯定這肯定是一種方式,但我無法弄清楚,現在我在這裏呆了兩天。你知道任何方式「像循環」如何在這種情況下使用它? –

+0

好的。我編輯我的帖子旨在爲循環。 – Arrabidas92

+0

謝謝!我如何在我的代碼中使用它來循環「項目」並獲得它們的值? –